2016-08-04 67 views
3

我在我的項目中使用了Wikipedia API。我循環一個對象來顯示下面的文章和它的描述。隱藏帶有未定義字符串的列表項目

enter image description here

大多數(文章)顯示正常,但有些人的表現不確定。這樣

enter image description here

我要的是隱藏 S的顯示不確定。我寫了代碼

// Check if some articles are undefined if so hide them 
if (articleAuthor === "undefined") { 
    $('.articleItem').html(''); 
} 

但它沒有幫助。 $('。articleItem')是動態創建列表項。

Codepen Project Link

提前感謝!

+0

你試過和'undefined'比較沒有引號嗎? 'undefined'是javascript中的一個對象 – niyasc

+0

'if(articleAuthor ===「undefined」)'意思是'articleAuthor'等於'undefined',類型爲'string'。 –

回答

2

你需要做兩件事情:

首先,切換您if (articleAuthor === "undefined")if (typeof articleAuthor === "undefined")

其次,切換您$('.articleItem').html('');$('.articleItem').last().html('');

你也可以換,該domCache.$wikiArticlesList.append如果(if (typeof articleAuthor === "undefined")),並不會在所有創建的項目。

+0

謝謝,我感謝你的幫助! – NZMAI

3

我認爲你需要刪除的元素:

if (articleAuthor === undefined) { //remove the quote from undefined 
    $('.articleItem').remove(); 
} 

但上述方法將刪除所有articleItem,並且做的是檢查不未定義,創造elments的最佳方式:

if(articleAuthor !== undefined){ 
    //create element logic here 
} 
+1

謝謝,我會牢記它! – NZMAI

2

這裏這個選擇:

$('.articleItem') 

將返回類的articleItem'元素的列表,你必須循環它改變所有項目的HTML

2

使用typeof檢查undefined或不

if (typeof articleAuthor === "undefined") { 
    $('.articleItem').html(''); 
} 
+0

看來這種方法有效!謝謝! – NZMAI

+0

@NZMAI:很高興幫助 –