Call Jquery function

前端 未结 4 1088
一整个雨季
一整个雨季 2021-02-04 06:53

I have a Jquery function like the following

function myFunction(){  
            $.messager.show({  
                title:\'My Title\',  
                msg:\'         


        
相关标签:
4条回答
  • 2021-02-04 06:55

    calling a function is simple ..

     myFunction();
    

    so your code will be something like..

     $(function(){
         $('#elementID').click(function(){
             myFuntion();  //this will call your function
        });
     });
    
      $(function(){
         $('#elementID').click( myFuntion );
    
     });
    

    or with some condition

    if(something){
       myFunction();  //this will call your function
    }
    
    0 讨论(0)
  • 2021-02-04 07:09

    To call the function on click of some html element (control).

    $('#controlID').click(myFunction);
    

    You will need to ensure you bind the event when your html element is ready on which you binding the event. You can put the code in document.ready

    $(document).ready(function(){
        $('#controlID').click(myFunction);
    });
    

    You can use anonymous function to bind the event to the html element.

    $(document).ready(function(){
        $('#controlID').click(function(){
             $.messager.show({  
                title:'My Title',  
                msg:'The message content',  
                showType:'fade',  
                style:{  
                    right:'',  
                    bottom:''  
                }  
            });  
        });
    });
    

    If you want to bind click with many elements you can use class selector

    $('.someclass').click(myFunction);
    

    Edit based on comments by OP, If you want to call function under some condition

    You can use if for conditional execution, for example,

    if(a == 3)
         myFunction();
    
    0 讨论(0)
  • 2021-02-04 07:13

    Try this code:

    $(document).ready(function(){
        $('#YourControlID').click(function(){
            if() { //your condition
                $.messager.show({  
                    title:'My Title',  
                    msg:'The message content',  
                    showType:'fade',  
                    style:{  
                        right:'',  
                        bottom:''  
                    }  
                });  
            }
        });
    });
    
    0 讨论(0)
  • 2021-02-04 07:17

    Just add click event by jquery in $(document).ready() like :

    $(document).ready(function(){
    
                      $('#YourControlID').click(function(){
                         if(Check your condtion)
                         {
                                 $.messager.show({  
                                    title:'My Title',  
                                    msg:'The message content',  
                                    showType:'fade',  
                                    style:{  
                                        right:'',  
                                        bottom:''  
                                    }  
                                });  
                         }
                     });
                });
    
    0 讨论(0)
提交回复
热议问题