2011-03-14 61 views
0

我正在研究一個page,我在其中動態創建一個Facebook Like按鈕,當用戶單擊一個圖片圖標時。 (由於頁面上有很多項目,如果我沒有這樣做,頁面加載就會嚴重抓取。)。jquery not()has()

如果我螢火蟲中運行以下命令:

var $clickTrackerGroup= $("#click-tracker-91"); 
console.log ($(".user-message-block", $clickTrackerGroup).html()); 
if ($(".user-message-block:has(div.hbo-message_likeBtn)", $clickTrackerGroup)) { 
     console.log("this group DOES NOT have an iframe"); 
} else { 
     console.log("this group DOES have an iframe"); 
    } 

我得到這個:

<div class="image-container"> 
    <img src="http://graph.facebook.com/1422524707/picture" id="person-222-clicked" alt="AMANDA H" title="AMANDA H" width="50px" height="50px"></div> 
    <div class="user-message"> 
     <span class="message-text"> 
      "ipsum lorem....."</span> 
      <div class="message-create-time"> 
       <span class="create-time">1/28/2011 7:33:17 AM</span> 
       <span class="user-name"> - Amanda H.</span> 
      </div> 
    </div> 
    <div class="hbo-message_likeBtn"> 
     <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.honeybunchesofoats.com%2FBunchesOfLove.aspx%2FMessage%2F250&amp;layout=standard&amp;show_faces=false&amp;width=250&amp;action=like&amp;colorscheme=light&amp;height=35" 
      style="border: medium none ; overflow: hidden; width: 250px; height: 35px;" allowtransparency="true" 
      frameborder="0" scrolling="no"></iframe> 
     </div> 

this group does not have an iframe 

我也試過:

if ($(".user-message-block:has(iframe)", $clickTrackerGroup)) { 
    console.log("this group DOES have an iframe"); 
} else { 
    console.log("this group DOES NOT have an iframe"); 
    } 

你可以清楚地看到,HTML的此塊事實上,它有一個iframe。

如何成功測試此元素的存在?

感謝, 斯科特

回答

2

如何:

if ($(".user-message-block iframe", $clickTrackerGroup).length === 0) { 
    // no iframe selected => iframe does not exist 
    console.log("this group does not have an iframe"); 
} 

參考.length

+0

是的,這個工程。留言Merci!我可以發誓,我嘗試了很多次迭代之前,但謝謝。 – Scott 2011-03-14 21:58:09

+0

@Scott:不客氣:) – 2011-03-14 21:58:39

0

嘗試

if ($(".user-message-block:has('iframe')", $clickTrackerGroup)) { 
     console.log("this group does not have an iframe"); 
} 
+0

沒有,這並不工作 – Scott 2011-03-14 21:56:08

+0

這將始終回報'TRU e'。 – 2011-03-14 21:57:29

+0

爲什麼?因爲測試真的「是jQuery能夠運行這個選擇器嗎?」 – Scott 2011-03-14 22:02:43

相關問題