javascript
  • jquery
  • image
  • html5-canvas
  • onclicklistener
  • 2012-08-17 113 views 1 likes 
    1

    這是我使用添加圖像和關閉按鈕(與事件偵聽器)jQuery的onclick事件功能不能正常工作第二次

    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函數工作在過去的uploded圖像僅.. 或者如果我只添加一個圖像,然後刪除it..It工作正常.. 如果我添加更多然後1圖像它只適用於最後一次上升圖像的關閉按鈕.. 我在這裏使用kineticjs來實現HTML5畫布功能

    +0

    你試圖使用'在()的''而不是點擊()'?也許你在元素存在之前就綁定了事件。 – jbabey 2012-08-17 15:12:08

    +0

    您是否將整個腳本包裝在'$(document).ready(function(){'? – 2012-08-17 15:29:31

    +0

    )中仍然不能正常工作。可能是因爲我創建了一個動態關閉函數,事件操作會附加到最後上傳的..pic /關閉按鈕?不確定 – ashishashen 2012-08-21 11:54:31

    回答

    0

    試着把你的yoda變量放在函數之外,因爲當你刪除你的圖層變量時,你是還會除去yoda變量以及它的事件。

    +0

    onclick無法進入函數本身..我不認爲它與函數內的變量有關 – ashishashen 2012-08-21 08:12:30

    0

    也許它做一些帶有DOM娛樂,試着改變你的click()功能on()

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