Custom event in HTML, Javascript

后端 未结 2 1628
自闭症患者
自闭症患者 2020-12-19 04:57

I\'m looking to create a custom event in HTML, JS.


How can I create one such? \'

2条回答
  •  青春惊慌失措
    2020-12-19 05:15

    It's bad practice to use new Function and with, but this can be accomplished as follows: http://jsfiddle.net/pimvdb/72GwE/13/.

    function trigger(elem, name, e) {
        var func = new Function('e',
    
          'with(document) {'
        + 'with(this) {'
        + elem.getAttribute('on' + name)
        + '}'
        + '}');
    
        func.call(elem, e);
    }
    

    With the following HTML:

    
    

    then trigger the event like:

    trigger(document.getElementById('x'), 'custombind', {foo: 123});
    

提交回复
热议问题