[英]jQuery doesn't recognize a class change
好的,我有一个编辑按钮,当我按下它时,它变为“完成”按钮。
一切都由jQuery完成。
$(".icon-pencil").click(function() {
var pencil = $(this);
var row = $(this).parent('td').parent('tr');
row.find('td').not(":nth-last-child(2)").not(":last-child").each(function() {
$(this).html("hi");
});
pencil.attr('class', 'icon-ok-sign');
});
// save item
$(".icon-ok-sign").click(function() {
alert("hey");
});
当我按下“编辑”(“ .icon-pencil
”)按钮时,其类将更改为.icon-ok-sign
(我在chrome控制台中可以看到),
但是当我单击它时,没有显示警报。
当我创建一个<span class="icon-ok-sign">press</span>
并在其上按下时,将显示警报。
怎么解决呢?
尝试使用$( document ).on( "click", ".icon-ok-sign", function() {...
那是因为您不能为将来的元素注册点击事件,所以您必须这样做:
$(document).on('click', '.icon-ok-sign', function() {
alert('hey');
});
此方法提供了一种将委派事件处理程序附加到页面的document元素的方法,从而可以在将内容动态添加到页面时简化事件处理程序的使用。
使用以下脚本:
$(document).on('click','.icon-ok-sign',function(){
alert("hey");
});
尝试这个:
$(".icon-pencil").click(function() {
var pencil = $(this);
var row = $(this).parent('td').parent('tr');
row.find('td').not(":nth-last-child(2)").not(":last-child").each(function() {
$(this).html("hi");
});
pencil.removeAttr('class').addClass('icon-ok-sign');
});
// save item
$(".icon-ok-sign").click(function() {
alert("hey");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.