Does Dojo have an equivalent to jQuery.trigger()?

后端 未结 7 1056
情书的邮戳
情书的邮戳 2020-12-11 02:11

In jQuery, you can do this:

$(\'#myElement\').trigger(\'change\');

How do I do that in Dojo?

相关标签:
7条回答
  • 2020-12-11 02:46

    The dojo on.emit method (1.7+) can be used to trigger an event on a dom node. From the documentation page:

    require(["dojo/on"], function(on){
        // register event handler
        on(target, "mouseup", function(e){
            // handle event
        });
    
        // Send event
        on.emit(target, "mouseup", {
            bubbles: true,
            cancelable: true
        });
    });
    
    0 讨论(0)
  • 2020-12-11 02:47

    As mention in the last comment, access dijit as pure DOM Object via dom API.

    require(["dojo/dom",
            'dojo/on',
            "dojo/domReady!"], function (dom, on) {
    
            //Does not work
            //registry.byId('myButton') 
            //registry.byId('myButton').domNode
      
            //Proper way
            on.emit(dom.byId('myButton'), "click", {
                    bubbles: true,
                    cancelable: true
            });
    
    });

    0 讨论(0)
  • 2020-12-11 02:50

    Yes, you can trigger an event on a DOM element in Dojo like this:

    dojo.byId("myElement").onChange();
    
    0 讨论(0)
  • 2020-12-11 02:59

    I don't think Dojo has similar functionality, at least as not as far as I know / can find. But you can use code like the following to replicate this functionality:

    dojo.addOnLoad(function() {
    
        var button = dojo.byId("myButton");
        dojo.connect(button, "onclick", function() { alert("Clicked!"); });
    
        // IE does things differently
        if (dojo.isIE)
        {
            button.fireEvent("onclick");
        }
        else
        { // Not IE
            var event = document.createEvent("HTMLEvents");
            event.initEvent("click", false, true);
            console.debug(event);
            button.dispatchEvent(event);
        }
    });
    

    A little more verbose, for sure, but you would be able to create your own Dojo version of trigger() with it.

    Try it out

    0 讨论(0)
  • 2020-12-11 03:04

    I recently stumbled upon Dojo's publish/subscribe mechanism, and I think this is the counterpart to jQuery's bind/trigger.

    Links:

    • Events with Dojo (v1.6)
    • dojo.publish reference guide
    0 讨论(0)
  • 2020-12-11 03:09

    PlugD has dojo.trigger and more: https://github.com/phiggins42/plugd

    0 讨论(0)
提交回复
热议问题