我想在点击 div 中的某个类时触发某些内容。
我试过了
$("div .event").click(function() {
alert($( this ).text());
});
还有
$("div").on("click", $('.event'), function() {
alert($( this ).text());
});
//Or
$(".SimpleCalendar .event").click(function() {
alert($( this ).text());
});
//I do not even know what this is supposed to do ...
$(".SimpleCalendar tbody tr td div .event").click(function() {
alert($( this ).text());
});
还有更多,但仍然无法弄清楚为什么这不起作用
最佳答案
您选择的 div
具有类 .event
,而不是它的后代。因此,正确的选择器是div.event
。试试这个:
$("div.event").click(function() {
alert($( this ).text());
});
或者只是:
//Warning: if elements unlike the div also have the event class then stick to
//the above as the selector is more specific
$(".event").click(function() {
alert($( this ).text());
});
并且不要忘记每个选项都应该在 DOM 中准备好,如下所示:
$(function() {
$("div.event").click(function() {
alert($( this ).text());
});
});
关于javascript - 点击事件上的 JQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32655277/