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

查看:100
本文介绍了点击事件不适用于动态添加的Li-> span-> id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Click event not working for dynamically added 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");
});

这不工作。

推荐答案

这里有一个缺少的引号,导致浏览器误解该ID:

You have a missing quote in there, which is causing the browser to misinterpret the ID:

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

您还必须确保在元素注入body后添加处理程序。如果元素不在周围,它不会注册点击事件。

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.

这篇关于点击事件不适用于动态添加的Li-&gt; span-&gt; id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆