我有一個HTML圖像元素,如下所示:克隆HTML元素與活動
var actionImage = $("<img></img>");
actionImage.attr("id", getRandomString());
actionImage.attr("width", "16").attr("height", "16").attr("src", "Action_normal.png");
actionImage.attr("style", "cursor: hand;");
actionImage.bind('mouseover', function() {
OnMouseOver($(this).get(0));
});
當我克隆該元件I鬆開鼠標懸停事件。我怎樣才能確保這些事件也被克隆。
'$(this).get(0)'是錯誤的。這與完成「this」完全相同。嘗試:'alert($(this).get(0)=== this); // true' – 2012-02-17 23:23:39
您也可以考慮通過傳遞一個對象來設置屬性,從而將5'.attr()'調用減少爲只有一個。 'actionImage.attr({id:getRandomString(),width:16,height:16 ...})' – 2012-02-17 23:27:57