从jquery中触发本机js事件

从jquery中触发本机js事件

问题描述:

是否有反正我可以使用本地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

each()中的this的值是DOM元素本身。你可以通过这种方式直接访问它。 无需$()

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

使用this.fireEvent("onmove");

this使用each迭代时已经指出的DOM元素。

再次包住你为什么要创建一个jQuery对象,你的原生元素?为什么你需要一个 “原生事件”

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