2016-02-07 47 views
0

我使用追加到圖像添加到<p id="p10">無法訪問elment追加

$('#p10').append("<img src='plus.png' id='clicker' />"); 

我想引用我已經通過與觸發追加添加的圖片後:

$('#clicker').toggle(function() { 
     // toggle code here 
}, function() { 
    // toggle code here 
}); 

我無法通過id訪問圖像元素以使用切換。如果我將切換註釋掉,圖像會顯示在頁面上。只要我添加切換代碼,圖像就會閃爍一下,然後消失。

+0

你到底想幹什麼? – Azim

+0

開啓和關閉圖像 – Dman100

+0

@ Dman100,點擊或其他事件發生切換嗎? – Victor

回答

0
var imgTag = "<img>"; 
    imgTag.attr('src','plus.png'); 
    imgTag.attr('id','clicker'); 
    $('#p10').append(imgTag); 

    $(imgTag).toggle(function() { 
      // toggle code here 
    }, function() { 
     // toggle code here 
    });