简体   繁体   English

点击时动态添加的按钮不起作用

[英]dynamically added button on click not working

I'm building a function in my own image browser that creates and displays a delete button when the user hovers the cursor over a certain image's div and hides the button when the user hover the mouse out of the div. 我正在自己的图像浏览器中构建一个函数,当用户将光标悬停在某个图像的div上时创建并显示一个删除按钮,而当用户将鼠标悬停在div上时隐藏该按钮。

this is the code: 这是代码:

     function displayImages() {
        //run through array of images
        for (a = 0; a < images.length; a++) {

var container = document.createElement('div');
                container.id = 'container'+images[a];
                container.style.width = 120;
                container.style.backgroundColor = '#e9e9e9';
                container.style.height = 140;
                container.style.margin = 5;
                container.style.display = 'inline-block';
                container.style.float = 'left';
                var imageID = images[a];
                container.onmouseover = function() {imageRollover(this)};
                container.onmouseout = function() {imageMouseOut(this)};
    }       
   }


function imageRollover(image) {
        var trashcan = document.createElement('BUTTON');
        trashcan.id = 'trash'+image.id;
        trashcan.className = 'deleteButton';
        trashcan.onclick = function() {deleteImage(image.id);};
        document.getElementById(image.id).appendChild(trashcan);
    }



function imageMouseOut(image) {
    document.getElementById(image.id).removeChild(document.getElementById('trash'+image.id));
        }

function deleteImage(image) {
        alert(image);
    }

My problem is, that when I click trashcan , it calls nothing. 我的问题是,当我单击trashcan ,它什么也没叫。 I already tried to add the onlick event normally: trashcan.onclick = deleteImage(image.id); 我已经尝试正常添加onlick事件: trashcan.onclick = deleteImage(image.id); But then, for some reason, is calls the function when I hover my mouse over the container . 但是由于某种原因,当我将鼠标悬停在container时,会调用函数。

How do I make sure that on click events for dynamically added rollover buttons work? 如何确保动态添加的翻转按钮的单击事件起作用?

The function can de viewed on: http://www.imaginedigital.nl/CMS/Editor/ImagePicker.html or http://jsfiddle.net/f239ymos/ 可以在以下位置查看该功能: http : //www.imaginedigital.nl/CMS/Editor/ImagePicker.htmlhttp://jsfiddle.net/f239ymos/

Any help would be highly appreciated. 任何帮助将不胜感激。

You are forcing me to guess here(not giving a fiddle), and create a scenario of my own but from what I understand, you want a button to appear no hover, and when pressed to delete the image so here its a working fiddle 您是在强迫我猜测(不给小提琴),并创建自己的场景,但据我了解,您希望按钮不出现悬停,并且在按下按钮删除图像时, 这才是可行的小提琴

hover functionality 悬停功能

$((document.body).on('mouseenter', '.test', function(){
    console.log('here');
    $(this).children('.test .x_butt').show();
});
$(document.body).on('mouseleave', '.test', function(){
    $('.test .x_butt').hide();
});

remove functionality 删除功能

$(document.body).on('click', '.x_butt', function(){
    $(this).parent().remove();
});

PS as for your dynamically added divs issue, the $(selector1).on('click','selector2', function() {...}); 关于动态添加的div问题的PS$(selector1).on('click','selector2', function() {...}); deals with it, as long as selector1 is not added dynamically. 处理它,只要没有动态添加selector1 ( selector2 would be the div you want the function to be on) demo with dynamically added elements ( click clone ) selector2将是您要启用该功能的div) 具有动态添加的元素的演示 (单击clone)

First change 第一次改变

window.onload = loadImages(); to window.onload = loadImages; window.onload = loadImages;

Then since you pass an object you can change 然后,因为您传递了一个对象,所以可以更改

function imageMouseOut(image) {
    document.getElementById(image.id).removeChild(document.getElementById('trash'+image.id));
}

to

function imageMouseOut(image) {
    image.removeChild(image.childNodes[0]);
}

However why not just hide and show the trashcan? 但是,为什么不隐藏并显示垃圾桶呢? Much cleaner 清洁得多

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

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