gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:01:05 25 4
gpt4 key购买 nike

得到这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com