javascript - jQuery:思考 on() 而不是 live() ,在动态生成的东西上传播

标签 javascript jquery html dynamic propagation

得到这个 HTML:

<div id="mother">
   <select id="sel" name="sel">
     <option value="1">Un</option>

     <option value="2">Deux</option>
     <option value="3">Trois</option>
</select>

<input id="in" name="in"/>
</div>

mother 在页面中,但是 children,selectinput 都是动态生成的

试着用类似的方式给他们打电话:

$('body').on('blur change','#mother input, #mother select',function(){
    alert($(this).attr('id'))
    })

Javascript 什么都没说..

如何使用 on() 而不是 live() 来最好地做到这一点? ...因为使用 live() 更容易理解。

谢谢。

最佳答案

.live() 已弃用并删除,根本不要使用它 version deprecated: 1.7, removed: 1.9

像这样使用

$('body').on('blur change', '#mother input, #mother select', function () {
    alert($(this).attr('id'));
});

关于javascript - jQuery:思考 on() 而不是 live() ,在动态生成的东西上传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17388961/

相关文章:

javascript - jquery 转义方括号以选择元素

javascript - JQuery 插件不会在模态内工作

javascript - JSON - 搜索具有变量名称的键(未知)

html - SharePoint 2010 MasterPage 页脚 - 自定义表格

javascript - 为什么 Node 请求的正文总是字符串?

javascript - 克隆/复制 html 节点,包括它们的样式

javascript - 使用地理定位确定与固定点的距离

javascript - ReactJS Props 和冒泡

jquery - 在 img-container 上垂直/水平居中文本

HTML/CSS - li 和 ul 重叠问题