简体   繁体   English

Click事件不适用于动态添加的Li-> span-> id

[英]Click event not working for dynamically added Li->span->id

Click event not working for dynamically added Li->span->id Click事件不适用于动态添加的Li-> span-> id

    case"101" :   strLink = "<li style='cursor:pointer'><a data-scroll><span id=div101'>Form 101</span></a></li>"; $('.sidebar-nav').append(strLink); break;
---------------
$(".sidebar-nav").on("click", "#div101", function ()
{
      console.log("Clicked on div101");
});

This is Not working. 这是行不通的。 How can i make it work? 我该如何运作?

You have a missing quote in there, which is causing the browser to misinterpret the ID: 您在其中缺少引号,这导致浏览器误解了ID:

case"101" :   strLink = "<li style='cursor:pointer'><a data-scroll><span id='div101'>Form 101</span></a></li>"; $('.sidebar-nav').append(strLink); break;

You also have to make sure to add the handler after the element has been injected into the body. 您还必须确保在将元素注入到主体之后添加处理程序。 If the element isn't around, it will not register the click event. 如果该元素不在周围,它将不会注册click事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM