gpt4 book ai didi

javascript - 使用JQuery点击类 ".x"的div时触发事件

转载 作者:行者123 更新时间:2023-11-28 04:08:10 25 4
gpt4 key购买 nike

JS:

$(".x").click(function()
{
alert("hello");
return false;
});


$("#otherdiv").html("<div class='x'>Drag and drop values on the right to create a pivot table.</div><br />");

CSS:

div.x
{
background-color: white;
border: dotted 1px;
cursor: pointer;
width: auto;
float: left;
position: absolute;
top: 15%;
left: 10%;
padding: 10px;
}

当我单击类为“x”的 div 时,没有任何反应。

最佳答案

您不能使用 click 附加事件处理程序,因为你的 <div>是动态添加的。 click方法仅适用于现有元素。你必须使用 live [ API Ref ]delegate [ API Ref ] 而不是。


更新:来自 jQuery 的 live文档:

As of jQuery 1.7, the .live() method is deprecated. Use .on()[API Ref] to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

关于javascript - 使用JQuery点击类 ".x"的div时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7033751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com