Multiple function call on html onclick [duplicate]

会有一股神秘感。 提交于 2021-01-27 04:36:48

问题


Is there any possibilities to use the onclick HTML attribute to call more than one jQuery functions

onclick="$('#editParentDetailsViews').removeClass('tab selected');$('#editStudentDetailsPopupPnl').hide(); return false;"

Here, the first function work properly and the 2nd function not working.


回答1:


I suggest you these ways:

1-addEventListener:

   document.getElementById('btn').addEventListener('click', function(){
    function1();
    function2();
   });

2-inline (onclick)

onClick="function1();function2();"

3-Jquery Standard click function:

 $("button").click(function(){
    //Your functions code here
});

4-Jquery on:

 $('#button').on('click',function(){
   //Your code here
 })

You can find more ways...




回答2:


Why dont you use jquery click() listener? Do not mix dom with jquery. Keep it simple.

$(yourselector).click(function(){
    //call some funtions
});



回答3:


<div id="yup">click me</div>

js

$(function(){ // same as $(document).ready()...
  $('#yup').on('click',function(){
   // call function 1
   // call function 2
   // call function 3
   // ...
  });
});

DEMO



来源:https://stackoverflow.com/questions/26943872/multiple-function-call-on-html-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!