jQuery没有为具有新id的新元素执行代码
jQuery not executing code for new element with new id
我有一个图像,当我点击它时,我希望它更改为另一个图像并更改其ID。然后,当我点击这个新图像时,它会恢复原状。
$(document).ready(function(){
$("#name_edit").click(function(){
$(this).attr("src", "img/tick.png");
$(this).attr("id","name_confirm");
});
$("#name_confirm").click(function(){
$(this).attr("src", "img/edit.png");
$(this).attr("id","name_edit");
});
});
我已经成功地完成了第一步,从#name_edit到#name_confirm。然而,情况并非相反。
我该如何解决这个问题?
我的怀疑是,由于我使用的是(document).ready,jQuery正在为页面上已经存在的元素做准备。但是,ID为name_confirm的元素在单击图像之前不存在
谢谢。
您正在处理的元素总是相同的。。。
$(document).ready(function(){
// use just the first id value to find it in the DOM
$("#name_edit").click(function(){
var item = $(this);
var id = item.attr('id');
if(id === 'name_edit') {
return item
.attr("src", "img/tick.png")
.attr("id","name_confirm")
;
}
return item
.attr("src", "img/edit.png")
.attr("id","name_edit")
;
})
;
});
我认为您的问题选择了糟糕的解决方案。1) 为什么您的代码不起作用:加载文档时,您只绑定了1次2个事件。因此,jquery找到#name_edit元素并在其上绑定onclick事件。但jquery找不到#name_confirm元素,因为它在文档中不存在)在代码中,您应该绑定1个onclick事件,但有一些attr(例如用于检查状态的类)。类似于:
<img id="main_image" class="name_edit"/>
<script>
var img_paths = ["img/tick.png", "img/edit.png"]
var img_index = 0;
$("#main_image").click(function(){
if($(this).attr("class") == "name_edit"){
$(this).attr("src", "img/tick.png");
$(this).attr("class","name_confirm");
}
else{
$(this).attr("src", "img/edit.png");
$(this).attr("class","name_edit");
}
});
</script>
- 其他解决方案:您可以创建2个图像并显示/隐藏它们。或者使用具有背景属性的样式。带有伪类或类。您还可以将图像路径存储在数组中,并在单击时勾选数组索引
类似于:
var img_paths = ["/content/img1.png", "/content/img2.png"]
var img_index = 0;
$("#main_image").click(function(){
$(this).src = img_paths[img_index];
img_index = !img_index;
})
它不起作用,因为你引用了相同的元素,试试这个:
(function(window, document, $, undefined){
$("#name_edit").on("click", function(){
var self = $(this);
if(self.attr("id") === "name_edit") {
self.attr("src", "img/tick.png");
self.attr("id", "name_confirm");
} else {
self.attr("src", "img/edit.png");
self.attr("id", "name_edit");
}
});
})(this, this.document, jQuery);
为了更容易理解代码,您可以使用这样的类:
(function(window, document, $, undefined){
$(".name_edit").on("click", function(){
var self = $(this);
if(self.hasClass("name_edit")) {
self.attr("src", "img/tick.png");
self.removeClass("name_edit").addClass("name_confirm");
} else {
self.attr("src", "img/edit.png");
self.removeClass("name_confirm").addClass("name_edit");
}
});
})(this, this.document, jQuery);
为了简化替换类,您甚至可以添加自己的$.fn.replaceClass();
,如下所示:
jQuery.fn.replaceClass = function(classA, classB) {
this.removeClass(classA).addClass(classB);
return this;
};
然后这样使用:
(function(window, document, $, undefined){
$(".name_edit").on("click", function(){
var self = $(this);
if(self.hasClass("name_edit")) {
self.attr("src", "img/tick.png");
self.replaceClass("name_edit", "name_confirm");
} else {
self.attr("src", "img/edit.png");
self.replaceClass("name_confirm", "name_edit");
}
});
})(this, this.document, jQuery);
我可以确认其他人说了什么。。jquery在准备好文档的情况下运行,但随后不会更新,因此它基本上从dom中获取正确的元素,并分配click事件。它没有name_confirm的事件。
所以这个代码什么都不做。。。
$("#name_confirm").click(function(){
$(this).attr("src", "img/edit.png");
$(this).attr("id","name_edit");
});
在这个有指导意义的jsfiddle 中看它不起作用
id当然需要更改吗?例如,是否可以为img使用特定的类?然后您可以在类上进行第二次单击绑定。。。例如,请参阅这个工作示例,它仍然更改src和id…
Try-On方法:
$(document).on('click', '#name_edit', function(){
$(this).attr("src", "img/tick.png");
$(this).attr("id","name_confirm");
});
$(document).on('click', '#name_confirm', function(){
$(this).attr("src", "img/edit.png");
$(this).attr("id","name_edit");
});
- 使用html 5对元素执行自定义表单验证功能
- 对未知数量的元素执行Jquery循环
- jQuery没有为具有新id的新元素执行代码
- Javascript - 在创建时对数组元素执行操作
- jQuery :对页面上的每个元素执行相同的操作
- 单击对内部元素执行某些操作
- 循环遍历 json 子对象并对每个“元素”执行某些操作
- Javascript 数组映射“出现”以对缺少的元素执行回调
- 无法对动态创建的 html 元素执行单击事件
- 使用 IF 运算符对给定数组中的字母数字和非字母数字元素执行不同的操作
- 为每个包含类的元素执行 jQuery 函数
- 对多个元素执行回调函数
- 即使在类名上触发了click事件,也要对特定元素执行某些操作
- SharePoint<按钮>元素执行页面的不需要的重新加载
- 无法对五个画布元素执行相同的效果
- 如何基于URL对聚合物元素执行select()方法
- 浏览器是否对屏幕元素执行相应的Javascript选择
- 侦听.on()中的shift键,然后使用附加侦听器的元素执行一个函数
- 如何使用JavaScript延迟为数组中的每个元素执行的函数
- 如何使某个javascript函数在页面加载时对其所有元素执行