2012-03-29 75 views
2

是否有反正我可以使用本地JavaScript來觸發一個名爲onmove的事件,這個事件僅適用於IE。 如果我這樣做,我可以讓它工作。從jquery中觸發本機js事件

document.getElementById("device-module").fireEvent("onmove"); 

這裏的問題是我不想將id傳遞給自定義的jquery原型。我已經知道元素是什麼,我只需要在該元素上調用JavaScript本地fireEvent。 這是可能的,或者我必須始終通過document.getElementById()獲取dom元素,然後才能使用js native fireEvent方法。

$.fn.custom = function() { 

    return this.each(function() { 
      var _this = $(this); 
     _this.fireEvent("onmove"); 

     }); 

}; 
+1

 var _this = $(this); 

只是用它? – Bergi 2012-03-29 15:06:12

回答

2

each()中的this的值是DOM元素本身。你可以通過這種方式直接訪問它。 無需$()

return this.each(function() { 
    //the value of "this" in here IS the DOM element 
    this.fireEvent('onmove'); 
}); 
1

使用this.fireEvent("onmove");

this使用each迭代時已經指出的DOM元素。

0

再次包住你爲什麼要創建一個jQuery對象,你的原生元素?爲什麼你需要一個 「原生事件」

 return this.each(function() { 
      this.fireEvent("onmove"); 
     });