2013-02-13 19 views
0

獲取iframe的內容被點擊後,它被過濾以獲得具有自定義屬性data-type = filled的第一個元素。但是我不能在這個特定的元素上應用一個類。如何將css類應用於具有特定屬性值的iframe的單擊元素?

這裏是我的代碼:

var clicked_content = event.target.outerHTML, // gives the content being clicked 

content = $(clicked_content).find("*[data-type='filled']:first").andSelf().html(); // this gives me the required content 

// this was supposed to add a class to particular element 
content.parent.addClass("highlight"); 

我也沒試過這樣來做:

$(event.target).children().find("*[data-type='filled']:first").andSelf().addClass('highlight'); 

回答

5

outerHTMLhtml()返回字符串。一個字符串沒有父項。

也許你想

$(event.target).find("*[data-type='filled']:first").andSelf() 
    .parent().addClass("highlight"); 

注意andSelf已被棄用,與addBack取代。

如果你嘗試在應用類「定製的第一個元素屬性數據類型=裝」,那麼你應該做

$(event.target).find('[data-type=filled]').eq(0).addClass("highlight"); 

編輯:只要你想也符合被點擊元素,如果它有適當的數據類型,那麼我建議

$(event.target).find('*').addBack().filter('[data-type=filled]').eq(0) 
    .addClass("highlight"); 
+0

那麼什麼是替代和自我? – atif 2013-02-13 09:45:03

+0

我給出了一個鏈接給出了一個選擇,但我不明白你爲什麼使用它開始。 – 2013-02-13 09:46:07

+0

第二個代碼片段似乎可行,但它有一點侷限性.i.e。它有效,當有多個元素點擊,但是當單個元素被點擊時它不起作用 – atif 2013-02-13 10:01:21

相關問題