手动触发dom节点事件代码

摘要:
在代码爬行的过程中,我遇到了一个奇怪的问题。您需要手动修改select的值,然后手动触发select的更改事件。但是,在网络上找到的trigger和onchange()事件不会被触发,因此您别无选择,只能继续搜索。幸运的是,我在stackoverflow网站上看到了外国神的答案,并解决了遇到的问题。原始邮寄地址:http://stackoverflow.com/questions/2490825/how-to-t

在爬代码过程中,碰到一个稀奇古怪的问题。需要手工修改select的值,然后手动触发select的change事件,但使用网络上查到的通过trigger、onchange()事件触发都不执行,没办法,只好继续查找。

还好在stackoverflow网站上看到国外大神的回答,解决了碰到的问题。

原帖地址:http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript

手工触发的代码如下:

/**
 * 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);

        var bubbles = eventName == "change" ? false : true;
        event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.

        event.synthetic = true; // allow detection of synthetic events
        node.dispatchEvent(event, true);
    } else  if (node.fireEvent) {
        // IE-old school style
        var event = doc.createEventObject();
        event.synthetic = true; // allow detection of synthetic events
        node.fireEvent("on" + eventName, event);
    }
};

html代码和调用如下

<a href="http://www.google.com" onclick="alert('clicked')" target="_blank">Go to google</a>
<button>Trigger event</button>

document.getElementsByTagName("button")[0].onclick = function() {
    fireEvent(document.getElementsByTagName("a")[0], "click");
}

免责声明:文章转载自《手动触发dom节点事件代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jsp添加背景音乐redis的pub/sub命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

k8s集群上删除pod及service

删除k8s集群中的pod:找到pod的名称空间,并根据名称空间删除pod1。首先删除pod2,然后删除相应的部署。否则,删除pod是无用的。您还将看到pod,因为deployment.yaml文件中定义的副本数如下:delete the pod[root@test2~]#kubectlgetpod-njenkinsNAMEREADYSTATUSRESTART...

vSphere HA 原理与配置

应当基于可用性需求和群集的特性选择vSphereHA接入控制策略。...

oracle触发器调试

如果触发器执行成功,不会出现第4个图,不成功,会出现数据调试信息,具体报错位置会定位到。F7单步执行4.出错时,会出现调试数据,双击调试数据,可以复制出来...

SecureCRT优化调整、永久设置、保护眼睛和配色方案

您可以根据个人喜好调整字体大小。我已经习惯了4号字体。到目前为止,SecureCRT优化已经完成。...

Nacos开机自启

1.加入玉米片。服务文件vi/lib/systemd/system/nacos.service2.将以下内容写入nacos。服务文件ps:我的nacos路径是/usr/local/nacos[Unit]Description=nacosAfter=network。target[Service]Type=forkingExecStart=/usr/local/...

Axure RP 8 注册码 更新了

升级8.1.0.3381后,您需要使用以下注册码http://www.raedme.cn/keys/316.htmlLicense:zdfansKey:fZw2VoYzXakllUuLVdTH13QYWnjD6NZrxgubQkaRyxD5+HNMqdr+WZKkaa6IoE5N许可证:zd423Key:LrZoHMetrL7OK8XOVWgvTFn+XOR...