简体   繁体   English

与JQuery的.on(多个鼠标事件)相对应的JavaScript

[英]JavaScript counterpart to JQuery's .on( multiple mouse events)

I am trying to convert an old app from JQuery to JavaScript. 我正在尝试将旧的应用程序从JQuery转换为JavaScript。

What would be the JavaScript counterpart to this JQuery line of code? 与该JQuery代码行相对应的JavaScript是什么?

  this.element = el.on('click mouseenter mouseleave', (this._handleMouse.bind(this), this));

Many Thanks 非常感谢

Do you need this one maybe 你可能需要这个吗

object.onclick=function(){your code on click};

Works the same with mouseenter and mouseleave. 与mouseenter和mouseleave相同。 You can also give a function as value instead of making one without name. 您也可以给函数赋值,而不是赋一个不带名称的函数。

Note , not certain about purpose of second this at 注意,不能确定的第二个目的this

(this._handleMouse.bind(this), this)

?


Try creating an object having properties events array , element DOM element, _handleMouse event handler , utilizing Array.prototype.forEach() to iterate events array attaching event to settingas.element , with Function.prototype.bind() set to settings.element 尝试创建一个具有属性events数组, element DOM元素, _handleMouse事件处理程序的对象,利用Array.prototype.forEach()迭代将events数组附加eventsettingas.element ,并将Function.prototype.bind()设置为settings.element

 var settings = { events: ["click", "mouseenter", "mouseleave"], element: document.getElementsByTagName("div")[0], _handleMouse: function(e) { console.log(this.textContent, e.type) } }; settings.events.forEach(function(event) { settings.element["on" + event] = settings._handleMouse.bind(settings.element) }) 
 <div>abc</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM