简体   繁体   English

单击时添加元素并单击删除相同的元素

[英]Add element on click and delete same element on click

I have this code which adds markers on a jpg map dynamically based on where you click on the map image. 我有这个代码,它根据你点击地图图像的位置动态地在jpg地图上添加标记。 It creates the ids dynamically as well. 它还动态创建id。 I am trying to attach a click event to every dynamically created marker so that if the user clicks on the marker it will be deleted. 我正在尝试将click事件附加到每个动态创建的标记,以便在用户单击标记时将其删除。 The problem is that when they click on the marker it generates another marker. 问题是,当他们点击标记时,它会生成另一个标记。 I want the user to be able to place the marker on the map by clicking on the map and then if they want to delete the marker they can click on it again without generating a new marker when they click on the marker they want to delete. 我希望用户能够通过单击地图将标记放在地图上,然后如果他们想要删除标记,他们可以再次单击它而不会在他们单击要删除的标记时生成新标记。

Thank you in advance for all your help. 提前感谢您的帮助。

JS : JS:

$(document).ready(function(){
    var clicks = 0;
    $("#container").click(function(e) {
        e.preventDefault();
        var x = e.pageX - 38;
        var y = e.pageY - 60;
        var img = $('<img>');
        img.css('top', y);
        img.css('left', x);
        img.css('width',60);
        img.css('height',60);
        img.attr('src', 'images/location-marker1.png');
        img.attr('id', 'marker' + clicks);
        img.attr('class', 'marker' + clicks);
        img.appendTo('#container');

        $("#container").on("click", "img.marker" + clicks, function(){
            var id = $(this).attr('id');
            alert(id);
            $(this).remove();
        });
        clicks += 1;
    })
});

HTML : HTML:

<div id="container">
    <img src="images/floorplan1_fs.jpg">
</div>

Actually, all you need to do is to (1) use a common class, say marker , to identify inserted markers, and (2) move the event listener out of the outer click function, and let it stand on its own right: 实际上,你需要做的就是(1)使用一个公共类,比如marker ,来识别插入的标记,以及(2)将事件监听器从外部click功能中移出,让它独立存在:

$("#container").on("click", "img.marker", function(e) {
    $(this).remove();
    e.stopPropagation();
});

This not only greatly simplifies your script, as #container will now listen to click events emitted by all dynamically added img.marker elements, but the use of e.stopPropagation stops the click event from bubbling up to the parent, which causes a new child to be added when you click to remove one. 这不仅大大简化了您的脚本,因为#container现在将监听所有动态添加的img.marker元素发出的点击事件,但是使用e.stopPropagation阻止点击事件冒泡到父级,从而导致新的子级单击以删除一个时添加。

Here is a proof-of-concept example, with some minor changes to your base code: 这是一个概念验证示例,对您的基本代码进行了一些小的更改:

  1. There is no longer any need to track unique IDs 不再需要跟踪唯一ID
  2. Here we take advantage of the amazing chaining capabilities imparted by jQuery 在这里,我们利用了jQuery带来的惊人链接功能

 $(document).ready(function() { $("#container").click(function(e) { e.preventDefault(); var x = e.pageX - 38, y = e.pageY - 60, $img = $('<img>'); $img .css({ top: y, left: x, width: 60, height: 60 }) .attr({ src: 'https://placehold.it/60x60' }) .addClass('marker') .appendTo('#container'); }); $("#container").on("click", "img.marker", function(e) { $(this).remove(); e.stopPropagation(); }); }); 
 #container { position: relative; } #container .marker { box-shadow: 0 0 10px 10px rgba(0, 0, 0, .5); position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <img src="https://placehold.it/500x500"> </div> 

Just use a general class to attach the event and put it outside of the other click event : 只需使用通用类附加事件并将其置于其他click事件之外:

$(document).ready(function(){
    var clicks = 0;
    $("#container").click(function(e) {
        e.preventDefault();
        var x = e.pageX - 38;
        var y = e.pageY - 60;
        var img = $('<img>');
        img.css('top', y);
        img.css('left', x);
        img.css('width',60);
        img.css('height',60);
        img.attr('src', 'images/location-marker1.png');
        img.attr('id', 'marker' + clicks);
        img.attr('class', 'marker');
        img.appendTo('#container');

        clicks += 1;
    })
    $("#container").on("click", "img.marker", function(){
      var id = $(this).attr('id');
      alert(id);
      $(this).remove();
      e.stopPropagation();
    });
});

NOTE : If you want to improve the code you could use : 注意:如果您想改进可以使用的代码:

$('<img />', {  
    'class' : "marker",
    src     : 'images/location-marker1.png',
    css     : {
        top    : y,  
        left   : x, 
        width  : 60, 
        height : 60, 
    },
    on : {
        click: function() {
             $(this).remove();
        }
    }
}).appendTo('#container');

Hope this helps. 希望这可以帮助。

 $(document).ready(function(){ $("#container").click(function(e) { e.preventDefault(); $('<img />', { 'class' : 'marker', src : 'https://www.gobages.com/wp-content/uploads/2015/09/marker-reservoirs.png', css : { top : e.pageY - 37, left : e.pageX - 24, width : 30, height : 30, }, on : { click: function(e) { e.stopPropagation(); $(this).remove(); } } }).appendTo('#container'); }); }); 
 #container { position: relative; } #container .marker { position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <img src="http://geology.com/world/world-map-clickable.gif"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM