gpt4 book ai didi

javascript - 如何将点击事件绑定(bind)到表头内的 Div

转载 作者:行者123 更新时间:2023-11-30 07:02:41 26 4
gpt4 key购买 nike

<table class="table lsttabl">
<thead>
<tr>
<th><div id="NameLabel">Customer Name</div></th>
<th>Email</th>
<th>Registered</th>
<th id="Date1Label">Created Date</th>
<th id="Date2Label">Last Updated Date</th>
<th id="Date3Label">Expiry Date</th>
<th id="UserCountLabel">User Count</th>
<th>Active</th>
<th>Configure</th>
<th>Edit</th>
</tr>
</thead>

<tbody>
</tbody>
</table>

我正在尝试使用下面的代码绑定(bind)事件

$("#NameLabel").click(function () { alert("hello"); });

我尝试了很多变体,但它不工作..请分享您的知识。

最佳答案

如果此代码是在页面加载后动态添加的,您可能需要使用委托(delegate)函数。

$('body').delegate('click','#NameLabel',function(){
alert("hello");
});

这里我使用了 body 选择器,因为我可以确定 body 元素在页面加载时就在那里。如果有另一个父元素要将动态内容加载到其中,则可以使用它而不是将事件附加到正文。

关于javascript - 如何将点击事件绑定(bind)到表头内的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762872/

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