Trigger a button click from a non-button element

前端 未结 6 1975
遥遥无期
遥遥无期 2020-12-05 19:32

How would you trigger a click event from an element that supposedly does not have native clickable behaviours?

For example, I know that you could simply just use the

6条回答
  •  佛祖请我去吃肉
    2020-12-05 20:12

    You will need to feature detect as different browsers (using non-html5 doctype) can support different dom methods:

    var ele = document.getElementById('x');
    if(typeof ele.click == 'function') {
      ele.click()
    } else if(typeof ele.onclick == 'function') {
      ele.onclick()
    }
    

提交回复
热议问题