我想用其他代码替换一个代码,点击新代码后得到警报,但在我的代码中不工作警报。怎么解决?
演示: http://jsfiddle.net/9Ggsn/
HTML:
<select>
<option>select 1</option>
<option>select 2</option>
<option>select 3</option>
</select>
<div class="dfg"><a href="#">Test</a></div>
jQuery:
$('select').on('change', function(){
//alert('ook');
$('.dfg a').replaceWith('<a href="#" class="pon">Link</a>')
});
$('.pon').on('click', 'a', function(){
alert('ook');
});
最佳答案
那是因为在你的标记中 a.pon
没有类为 pon
的父元素,事件应该从元素或文档的静态父元素之一委托(delegate)对象。
改变:
$('.pon').on('click', 'a', function(){
收件人:
$(document).on('click', 'a.pon', function(){
或者:
$('.dfg').on('click', 'a.pon', function(){
关于javascript - ReplaceWith 并在 jQuery 中提醒消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14810426/