简体   繁体   English

如何影响多个动态元素中的一个

[英]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.

相关问题 如何实现hover效果? 具有 hover 的一个元素对另一个行为有影响吗? - How can I achieve hover effect? One element with hover influence on behaviour another? jQuery:是否可以制作一个元素的动画而不影响其他元素? - JQuery: Is it possible to make animation of an element without any influence on other elements? 如何淡入淡出 <li> 元素一一? - How to fade in dynamic <li> elements one by one? 我需要一页上的javascript才能影响另一页上的元素 - I need javascript on one page to influence elements on a different page 如何从Javascript的3个元素中获取一个元素 - How to get one element from 3 elements in Javascript 如何使用多个元素修改选定元素的一个元素? - How to modify one element of a selected element with multiple elements within? 如何在父可滚动元素中逐个滚动子元素? - How to scroll child elements one by one in parent scrollable element? 用jQuery删除动态元素后如何按名称和ID对元素重新排序? - How to reorder elements by name and id after removing dynamic element with jquery? 如何在动态模板中为组件中的元素之一添加助手功能 - How to add a helper function in a dynamic template for one of the elements in a component 如何从一个元素中的元素获取数据-Javascript - How to get data from elements inside one element - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM