本文介绍了带参数的触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这很烦人。我只想在javascript中触发一个事件。我需要像往常一样将事件对象传递给参数和一个附加的自定义参数。This is pretty annoying. I want to just trigger an event in javascript. I need to pass the event object into the parameters as usual and an additional custom parameter.在jQuery中,这非常简单:In jQuery, this would be super easy:$('#element').trigger('myevent', 'my_custom_parameter');但是我不想使用它。我发现与此相关的所有其他问题都建议使用使用jQuery!,这是针对我正在开发的插件,并且要求jQuery使用一种方法是很愚蠢的。谁能指出在跨浏览器兼容的香草JS中执行上述操作的方法?But I don't want to use this however. Every other question I have found relating to this has just suggested 'use jQuery!' It's for a plugin I'm developing and to require jQuery for one method is pretty silly. Can anyone point to a way to do the above in vanilla JS that's cross-browser compatible?推荐答案您可以创建自定义事件 http://jsfiddle.net/9eW6M/You may create custom events http://jsfiddle.net/9eW6M/ HTML<a href="#" id="button">click me</a> JSvar button = document.getElementById("button");button.addEventListener("custom-event", function(e) { console.log("custom-event", e.detail);});button.addEventListener("click", function(e) { var event = new CustomEvent("custom-event", {'detail': { custom_info: 10, custom_property: 20 }}); this.dispatchEvent(event);});单击链接后的输出:custom-event Object {custom_info: 10, custom_property: 20}可以在此处找到更多信息。 这篇关于带参数的触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 18:01