亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery如何將onClick事件綁定到動態添加的HTML元素

jQuery如何將onClick事件綁定到動態添加的HTML元素

jQuery如何將onClick事件綁定到動態添加的HTML元素我想將onClick事件綁定到我用jQuery動態插入的元素。但它從不運行綁定函數。如果您能指出這個示例不起作用的原因,以及如何讓它正常運行,我會很高興的:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da">        <head>          <title>test of click binding</title><script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>          <script type="text/javascript">        jQuery(function(){          close_link = $('<a class="" href="#">Click here to see an alert</a>');          close_link.bind("click", function(){            alert('hello from binded function call');            //do stuff here...          });            $('.add_to_this').append(close_link);        });          </script>        </head>        <body>          <h1 >Test of click binding</h1>          <p>problem: to bind a click event to an element I append via JQuery.</p>          <div class="add_to_this">            <p>The link is created, then added here below:</p>          </div>          <div class="add_to_this">            <p>Another is added here below:</p>          </div>        </body>        </html>
查看完整描述

3 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

所有這些方法都是不推薦的。您應該使用on方法來解決你的問題。

如果要以動態添加的元素為目標,則必須使用

$(document).on('click', selector-to-your-element , function() {
     //code here ....});

這將替換不受歡迎的.live()方法。


查看完整回答
反對 回復 2019-07-02
  • 3 回答
  • 0 關注
  • 3022 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號