如何触发JavaScript事件点击

我在我的页面有一个超链接。 我正在尝试自动化超链接上的一些点击以进行testing。 有什么办法可以模拟使用JavaScript的超链接50点击?

<a href="#" target="_blank" onclick="javascript:Test("Test");">MSDN</a> 

我正在寻找来自JavaScript的onClick事件触发器。

添加一个ID到你的链接

 <a href="#" target="_blank" id="my-link" onclick="javascript:Test('Test');">Google Chrome</a> 

并在您的JavaScript代码中调用它:

 var l = document.getElementById('my-link'); for(var i=0; i<50; i++) l.click(); 

这是我使用的: http : //jsfiddle.net/mendesjuan/rHMCy/4/

已更新为使用IE9 +

 /** * Fire an event handler to the specified node. Event handlers can detect that the event was fired programatically * by testing for a 'synthetic=true' property on the event object * @param {HTMLNode} node The node to fire the event handler on. * @param {String} eventName The name of the event without the "on" (eg, "focus") */ function fireEvent(node, eventName) { // Make sure we use the ownerDocument from the provided node to avoid cross-window problems var doc; if (node.ownerDocument) { doc = node.ownerDocument; } else if (node.nodeType == 9){ // the node may be the document itself, nodeType 9 = DOCUMENT_NODE doc = node; } else { throw new Error("Invalid node passed to fireEvent: " + node.id); } if (node.dispatchEvent) { // Gecko-style approach (now the standard) takes more work var eventClass = ""; // Different events have different event classes. // If this switch statement can't map an eventName to an eventClass, // the event firing is going to fail. switch (eventName) { case "click": // Dispatching of 'click' appears to not work correctly in Safari. Use 'mousedown' or 'mouseup' instead. case "mousedown": case "mouseup": eventClass = "MouseEvents"; break; case "focus": case "change": case "blur": case "select": eventClass = "HTMLEvents"; break; default: throw "fireEvent: Couldn't find an event class for event '" + eventName + "'."; break; } var event = doc.createEvent(eventClass); event.initEvent(eventName, true, true); // All events created as bubbling and cancelable. event.synthetic = true; // allow detection of synthetic events // The second parameter says go ahead with the default action node.dispatchEvent(event, true); } else if (node.fireEvent) { // IE-old school style, you can drop this if you don't need to support IE8 and lower var event = doc.createEventObject(); event.synthetic = true; // allow detection of synthetic events node.fireEvent("on" + eventName, event); } }; 

注意调用fireEvent(inputField, 'change'); 并不意味着它实际上会改变input字段。 input.value="Something"更改事件的典型用例是当您以编程方式设置字段,并且您希望调用事件处理程序,因为调用input.value="Something"不会触发更改事件。

什么

  l.onclick(); 

确实是调用lonclick函数,也就是说,如果你用l.onclick = myFunction;设置了一个l.onclick = myFunction; 。 如果你还没有设置l.onclick ,它什么都不做。 相反,

  l.click(); 

模拟点击并触发所有的事件处理程序,无论是添加了l.addEventHandler('click', myFunction); ,在HTML中,或以任何其他方式。

我感到非常羞愧的是,有太多不正确或未公开的部分适用性。

最简单的方法是通过Chrome或Opera(我的示例将使用Chrome)使用控制台。 在控制台中input以下代码(通常在1行中):

 var l = document.getElementById('testLink'); for(var i=0; i<5; i++){ l.click(); } 

这将产生所需的结果

使用testing框架

这可能会有所帮助 – http://seleniumhq.org/ – Selenium是一个Web应用程序自动testing系统。

您可以使用Firefox插件Selenium IDE创buildtesting

手动发射事件

要以正确的方式手动触发事件,您需要为不同的浏览器使用不同的方法 – el.dispatchEventel.fireEvent ,其中el将是您的Anchor元素。 我相信这两个都需要构造一个Event对象来传入。

另一种不是完全正确的,快速而肮脏的方式是这样的:

 var el = document.getElementById('anchorelementid'); el.onclick(); // Not entirely correct because your event handler will be called // without an Event object parameter. 

.click()不适用于Android(查看mozilla文档 ,在移动部分)。 您可以使用此方法触发点击事件:

 function fireClick(node){ if (document.createEvent) { var evt = document.createEvent('MouseEvents'); evt.initEvent('click', true, false); node.dispatchEvent(evt); } else if (document.createEventObject) { node.fireEvent('onclick') ; } else if (typeof node.onclick == 'function') { node.onclick(); } } 

从这个post

公正警告:

element.onclick()不像预期的那样运行。 它只运行onclick="" attribute的代码,但不触发默认行为。

我有单选button没有设置检查类似的问题,即使onclick自定义function运行良好。 必须添加radio.checked = "true"; 设置它。 可能是相同的,对于其他元素(在a.onclick()也应该有window.location.href = "url";

  angular.element(document.querySelector('selector'))[0].click() 

为我工作!

使用jQuery

 $(node).trigger("click");