简体   繁体   中英

Execute function without clicking in jquery

I would like that in my code after executing the function appendHtml immediately executing clicked on the added div to perform the function myFunction . This is my code:

function myFunction()
{
    $('#Page').on('click', '*', function(e)
    {
     //console.log('execute');
    }
}

function appendHtml()
{
   $('.btn').click(function(){
   $('#Page').append('<div>Test</div>');
});
}

Html :

 <button class="btn"> click </button>
    <div id="Page"></div>

You don't really need those functions wrapping the event listeners and they really don't make sense that way either

Not 100% sure why you want * as selector for your click event listener but you can do something like the following to trigger it

 $('#Page').on('click', '*', function(e) { console.log('execute'); }); $('.btn').click(function() { var $el = $('<div>Test</div>') $('#Page').append($el); $el.click() }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn"> click </button> <div id="Page"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM