use*_*523 193 javascript javascript-events
我的页面中有一个超链接.我试图自动化超链接的大量点击以进行测试.有什么方法可以使用JavaScript模拟超链接的50次点击吗?
<a href="#" target="_blank" onclick="javascript:Test("Test");">MSDN</a>
Run Code Online (Sandbox Code Playgroud)
我正在寻找来自JavaScript的onClick事件触发器.
ins*_* me 259
单击HTML元素:只需执行element.click().大多数主要的浏览器都支持这个.
要多次重复单击:向元素添加ID以唯一地选择它:
<a href="#" target="_blank" id="my-link" onclick="javascript:Test('Test');">Google Chrome</a>
Run Code Online (Sandbox Code Playgroud)
并.click()通过for循环调用JavaScript代码中的方法:
var link = document.getElementById('my-link');
for(var i = 0; i < 50; i++)
link.click();
Run Code Online (Sandbox Code Playgroud)
Jua*_*des 82
这是我使用的: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" (e.g., "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);
}
};
Run Code Online (Sandbox Code Playgroud)
请注意,调用fireEvent(inputField, 'change');并不意味着它实际上会更改输入字段.触发更改事件的典型用例是以编程方式设置字段并希望调用事件处理程序,因为调用input.value="Something"不会触发更改事件.
黄雨伞*_*黄雨伞 39
什么
l.onclick();
Run Code Online (Sandbox Code Playgroud)
确实正在调用onclick函数l,也就是说,如果你已经设置了一个l.onclick = myFunction;.如果你没有设置l.onclick,它什么都不做.相反,
l.click();
Run Code Online (Sandbox Code Playgroud)
模拟点击并触发所有事件处理程序,无论l.addEventHandler('click', myFunction);是以HTML格式还是以任何其他方式添加.
htm*_*rum 20
我非常惭愧的是,有太多不正确或未公开的部分适用性.
最简单的方法是使用控制台通过Chrome或Opera(我的示例将使用Chrome).在控制台中输入以下代码(通常在1行中):
var l = document.getElementById('testLink');
for(var i=0; i<5; i++){
l.click();
}
Run Code Online (Sandbox Code Playgroud)
这将生成所需的结果
Man*_*olo 12
.click()不适用于Android(在移动部分查看mozilla文档).您可以使用以下方法触发click事件:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
从这篇文章
使用测试框架
这可能会有所帮助 - http://seleniumhq.org/ - Selenium是一个Web应用程序自动化测试系统.
您可以使用Firefox插件Selenium IDE创建测试
手动触发事件
要以正确的方式手动触发事件,您需要为不同的浏览器使用不同的方法 - 无论是哪个el.dispatchEvent,el.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.
Run Code Online (Sandbox Code Playgroud)
请在任何地方调用触发函数,然后按钮就会点击。
<a href="#" id="myBtn" title="" >Button click </a>
function trigger(){
document.getElementById("myBtn").click();
}
Run Code Online (Sandbox Code Playgroud)
IE9 +
function triggerEvent(el, type){
var e = document.createEvent('HTMLEvents');
e.initEvent(type, false, true);
el.dispatchEvent(e);
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
var el = document.querySelector('input[type="text"]');
triggerEvent(el, 'mousedown');
Run Code Online (Sandbox Code Playgroud)
来源:https://plainjs.com/javascript/events/trigger-an-event-11/
| 归档时间: |
|
| 查看次数: |
489943 次 |
| 最近记录: |