jquery中對動態產生的標籤不會響應click事件,jqueryclick

來源:互聯網
上載者:User

jquery中對動態產生的標籤不會響應click事件,jqueryclick

Jquery中對ajax動態產生的html標籤不會響應 $(selector).click(function.. 或者$(selector).bind('click',function.., 需要用.live。

我們這樣繫結元素的click事件,以後jquery動態產生的元素,click事件也有效。

$('.clickme').live('click', function() {  alert("Live handler called."); });



JQuery裡的代碼不可以響應動態產生的html元素的click事件,怎解決?

你可以換個思路解決這種問題
不要監聽動態html元素,可以監聽這些元素的父節點,再使用jQuery的is方法判斷是不是想要的元素。
附上例子
<body>
<script type="text/javascript">
$(function(){
$("#aaa").click(function(e){
if($(e.target).is(".bbb")){
alert(1);
};
});
});
</script>
<table id="aaa" border="1">
<tr>
<td><input type="button" class="bbb" value="sadasd"> <a href="javascript:void(0)" class="bbb">asdasd</a></td>
</tr>
<tr>
<td><input type="button" class="bbb" value="sadasd"> <a href="javascript:void(0)" class="bbb">asdasd</a></td>
</tr>
</table>
</body>
 
jquery動態產生的元素自動執行一次click事件

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".d").live("click",function(){
alert("自動點選連結被點擊!");
});
$("#x").append("<a class='d' href='#'>自動點擊!</a>");;
$(".d").click();
});
</script>

<div id="x">
</div>

注意使用live綁定,動態產生的對象產生之後再執行click
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.