SORU
27 Mayıs 2011, Cuma


Nasıl simüle etmek için bir fare tıklaması JavaScript kullanarak?

Ben document.form.button.click() ilgili yöntem biliyorum. Ancak, özelliği olayı simüle etmek için nasıl bilmek istiyorum.

Bu kodu bir yere burada Yığın Taşması buldum ama nasıl kullanılacağını bilmiyorum :(

function contextMenuClick()
{
    var element= 'button'

    var evt = element.ownerDocument.createEvent('MouseEvents');

    evt.initMouseEvent('contextmenu', true, true,
         element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
         false, false, false, 1, null);

    element.dispatchEvent(evt);
}

Nasıl fare tıklaması bir olay JavaScript kullanarak ateş muyum?

CEVAP
27 Mayıs 2011, Cuma


(Versiyonu prototype.js olmadan çalışması için değiştirilmiş)

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
}

Bu gibi kullanabilirsiniz:

simulate(document.getElementById("btn"), "click");

Üçüncü parametre olarak iletebilirsiniz unutmayın 'seçenekler'. Belirtmek istemediğin seçenekleri defaultOptions (script altına bakınız) alınır. Bu yüzden, örneğin, fare belirtmek istiyorsanız gibi bir şey yapabilirsin koordinatları:

simulate(document.getElementById("btn"), "click", { pointerX: 123, pointerY: 321 })

Diğer varsayılan seçenekleri geçersiz kılmak için benzer bir yaklaşım kullanabilirsiniz.

Kredi kangax gitmeli. Here'nın orijinal kaynağına prototype.js belirli.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • atKristaBradford

    atKristaBrad

    4 Aralık 2010
  • buttheadgsxr1000

    buttheadgsxr

    24 Ocak 2008
  • Press Start

    Press Start

    4 HAZİRAN 2006