鍍金池/ 問答/HTML/ jquery on事件代理的一個(gè)問題

jquery on事件代理的一個(gè)問題

相關(guān)代碼

<div>

<a href="javascript:void(0);">11</a>
<a href="javascript:void(0);">22</a>
<a href="javascript:void(0);">33</a>

</div>

我是這樣綁定事件的:

$("div").on("click", $("a"), function(){

//怎么在這里獲得當(dāng)前點(diǎn)擊的a啊

});

回答
編輯回答
薄荷綠
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>
  <div>
    <a href="javascript:void(0);">11</a>
    <a href="javascript:void(0);">22</a>
    <a href="javascript:void(0);">33</a>
  </div>
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script> 
    $("div").on("click", 'a', function(){
      var currentItem = $(this)
      console.log(currentItem.text())
    });
  </script>
</body>
</html>

$("div").on("click", ***$("a")*, function(){ 里面的$("a") 應(yīng)該改成'a',這樣就能通過$(this)獲取到當(dāng)前點(diǎn)擊的對(duì)象啦

2017年7月27日 18:15