我一個開發一個應用程序中,我使用jQuery的類選擇
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但類選擇不工作。我的div class =「plblAgreeComment」正在動態創建,所以我使用.live()
請幫助我。
我一個開發一個應用程序中,我使用jQuery的類選擇
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但類選擇不工作。我的div class =「plblAgreeComment」正在動態創建,所以我使用.live()
請幫助我。
如果你正在使用jQuery版本較新,且包括了 「對」 使用:
$(document).on("click", "#plblAgreeProblem, .plblAgreeComment" ,function(){
alert("bil");
});
注意:如果可能的話,請嘗試綁定比「文檔」更接近的東西 - 例如標記的該部分的包裝等,以便處理速度問題。
其他答案覆蓋之前的版本
這裏是它的工作示例包:http://jsfiddle.net/MarkSchultheiss/pP6nL/
注意:我根據你的另一個問題做了一個瘋狂的假設,那就是兩個截然不同的選擇器,而不是另一個。是不正確的,如果您發佈標記,我們會改進。 – 2012-04-16 19:29:22
問題不是很清楚。對於選擇器,請嘗試使用以下內容。
$("#plblAgreeProblem, .plblAgreeComment").live("click", function(){
alert("bil");
});
或
$(".plblAgreeComment").live("click", function(){
alert("bil");
});
首先,你應該知道,live
在當前的jQuery版本過時,你應該使用on
代替。
這就是說,你的選擇$("#plblAgreeProblem",".plblAgreeComment")
,將搜索元素與與plblAgreeComment
class
的#plblAgreeProblem
id
的元素。它是你想寫的選擇器嗎?
另一個說明,id
必須是唯一的,這意味着頁面中不能多於一個元素具有相同的id
屬性。
喔..你在live
函數結束時忘了);
:
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
});
如果與類「plblAgreeComment」你的元素在裏面的節點「#plblAgreeProblem」嘗試使用不同勢選擇而不是:
$("#plblAgreeProblem .plblAgreeComment")
什麼不起作用? – Bergi 2012-04-16 19:21:43
你能提供你的HTML嗎? – 2012-04-16 19:22:42
警報不叫 – Billz 2012-04-16 19:22:54