繁体   English   中英

jQuery Onclick事件函数第二次不起作用

[英]Jquery Onclick Event function not working 2nd time

这是我用来添加图像和关闭按钮(带有事件监听器)的功能

function loadframe(img, imgid) {

    li = '';
    li += '<li><img src="' + img + '" width="100" height="120" /></li>';
    //li +='<a href="javascript:void(0)" onclick="removeajax(\''+img+'\','+imgid+')" >Close</a>';
    li += '<input type="button"  id="activate' + imgid + '" value="close">';

    document.getElementById('ulimage2').innerHTML += li;

}​              

我单击一些图像,然后将它们与关闭按钮一起加载到预览窗格中。

我在这里调用此函数。它包含一个名为

$('#activate' + imgid).click(function() {
    //alert(imgid)
    var yoda1 = stage.get("#" + imgid);
    layery.remove(yodaGroup1);
    layery.draw();
});​

这是全部功能。

function initStage1(images, imgid) {
    //    alert(imgid)
    var yodaGroup1 = "yodaGroup1" + imgid;
    yodaGroup1 = new Kinetic.Group({
        x: 100,
        y: 110,
        draggable: true,
        name: imgid
    });

    layery.add(yodaGroup1);
    stage.add(layery);
    var yoda1 = new Kinetic.Image({
        image: images.yoda1,
        x: 0,
        y: 0,
        width: 100,
        height: 120,
        id: imgid,
        name: "image",
        detectionType: "Pixel"
    });

    $('#activate' + imgid).click(function() {
        //alert(imgid)
        var yoda1 = stage.get("#" + imgid);
        layery.remove(yodaGroup1);
        layery.draw();
    });

    yodaGroup1.add(yoda1);
    yodaGroup1.on("dragstart", function() {

        yodaGroup1.moveToTop();
        layery.draw();
    });
    yodaGroup1.on("dblclick dbltap", function() {
        layery.remove(yodaGroup1);
        layery.draw();
    });
    yodaGroup1.on("dragend", function() {
        layery.draw();
        yoda1.saveImageData();
    });
    addAnchor(yodaGroup1, 0, 0, "topLeft");
    addAnchor(yodaGroup1, 100, 0, "topRight");
    addAnchor(yodaGroup1, 100, 120, "bottomRight");
    addAnchor(yodaGroup1, 0, 120, "bottomLeft");

    stage.draw();
    yoda1.saveImageData();
}​ 

此onclick仅适用于最后一张展开的图像。或者,如果我仅添加一张图像然后将其删除..它工作正常。如果我添加的图像超过1张,则仅适用于最后一张展开的图像的关闭按钮..我在这里将dynamicjs用于HTML5画布功能

尝试将yoda变量放在函数之外,因为删除图层变量时,也会同时删除yoda变量及其事件。

也许它与dom娱乐有关,请尝试将on() click()函数更改为:

$('body').on('click','#activate' + imgid,function() {
    //alert(imgid)
    var yoda1 = stage.get("#" + imgid);
    layery.remove(yodaGroup1);
    layery.draw();
});

暂无
暂无

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

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