在特定页面上模拟javascript单击事件

时间:2016-03-07 01:08:08

标签: javascript dom mouseevent

我知道点击模拟是一个非常非常标准的问题,但我无法弄清楚出了什么问题我在微软dom https://www.projectoxford.ai/demo/Emotion上应用以下脚本:

var l2 =  document.getElementsByClassName("imgUrl defaultvalue")[0];

l2.value = "http://i.telegraph.co.uk/multimedia/archive/03442/DEBATE_FIRST_PIC_3442872k.jpg"

function simulate(element, eventName)
{
    var options = extend(defaultOptions, arguments[2] || {});
    var oEvent, eventType = null;

    for (var name in eventMatchers)
    {
        if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
        throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent)
    {
        oEvent = document.createEvent(eventType);
        if (eventType == 'HTMLEvents')
        {
            oEvent.initEvent(eventName, options.bubbles, options.cancelable);
        }
        else
        {
            oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
            options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
            options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
        }
        element.dispatchEvent(oEvent);
    }
    else
    {
        options.clientX = options.pointerX;
        options.clientY = options.pointerY;
        var evt = document.createEventObject();
        oEvent = extend(evt, options);
        element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}

function extend(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
}

var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
}
var l =  document.getElementsByClassName("demo-bar-submit")[0];

simulate(l,"click");

上面代码中的每个命令都是在网站https://www.projectoxford.ai/demo/Emotion的命令行中一次完成的。这是否因某种原因而被阻止?尽管如此,当我点击按钮时确实发生了变化。

0 个答案:

没有答案