[英]how to influence the one element of the plurality of dynamic elements
http://jsfiddle.net/SergeyKozlov/tewyg2js/3/ http://jsfiddle.net/SergeyKozlov/tewyg2js/3/
I use dynamic insertion HTML code using button events: 我使用按钮事件使用动态插入HTML代码:
$("#NewItemImg").click(function(e) {
e.preventDefault();
itemCount++;
var element = $("\
<div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\
<div class=\"portlet-header ui-widget-header ui-corner-all\">img " + itemCount + "\
<span class='ui-icon ui-icon-closethick portlet-close'></span>\
</div>\
<div class=\"portlet-content\">\
<div class=\"form-group\">\
<input type=\"text\" class=\"form-control\" name=\"article[body][][img]\" id=\"ImageURL\" placeholder=\"Image URL\">\
</div>\
<button id=\"SetImageURL\" type=\"button\" class=\"btn btn-default\">Submit</button>\
<img id=\"PanelNewImageURL\" src=\"http://imgde.me/508f93cfb59b5.jpg\" alt=\"2014-12-10T18:04:41.762Z\" title=\"2014-12-10T18:04:41.762Z\" onerror=\"imgError(this);\">\
</div>\
</div>\
");
Then I try to delete the new dynamic element using the following code: 然后,我尝试使用以下代码删除新的动态元素:
$("body").on('click', '.column .portlet-close', function () {
$(this).closest( ".portlet" ).remove();
});
In this case it works. 在这种情况下,它可以工作。 The problem is how to influence the one element of the plurality of dynamic elements.
问题在于如何影响多个动态元素中的一个。
$("body").on('click', '.column #SetImageURL', function () {
//e.preventDefault();
// itemCount++;
$("#PanelNewImageURL").show();
$("#ImageURL").hide();
});
When I use this code, when you acquired any button, and acts only on the first. 当我使用此代码时,当您获得任何按钮时,仅作用于第一个按钮。
You are seeing this behavior because id
attributes must be unique, but you are using multiple instances of "SetImageURL" as an id. 因为
id
属性必须是唯一的,所以您看到此现象,但是您将“ SetImageURL”的多个实例用作id。 When jQuery selectors include an id reference in them (as it does here: on('click', '.column #SetImageURL' . . .
), it acts only on the first matched element with that id, and ignores any other elements that have it, as well. 当jQuery选择器在其中包含一个id引用时(如此处所示:
on('click', '.column #SetImageURL' . . .
)),它仅对具有该id的第一个匹配元素起作用,而忽略任何其他也要拥有它。
So, you will either need to figure out a way to make it work with a pure class selector, or a way to make the id
values unique and include those unique id values in the selector. 因此,您将需要找出使它与纯类选择器一起使用的方法,或者使
id
值唯一并在选择器中包括这些唯一id值的方法。 I'd go with the first approach . 我会采用第一种方法。 .
。 .
。 though, in either case, you should really make your
id
values unique. 但是,无论哪种情况,您都应该使
id
值唯一。 :) :)
EDIT: 编辑:
Looking at your code again, try this: 再次查看您的代码,请尝试以下操作:
$("body").on('click', '.column .btn-default', function () {
//e.preventDefault();
// itemCount++;
$(this).siblings("img").show();
$(this).parent().find(".form-control").hide();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.