简体   繁体   English

调用加载 function 加载

[英]call onload function onload

I want to call the function on load the我想在加载时调用 function

tag.Now I can't call the function.It removed the tag and replace it with on button click.标签。现在我无法调用 function。它删除了标签并用单击按钮替换它。

 $('.test_button').click(function(){ $('#test').remove(); $('.panel').append('<p id="test" onload="myFunction();">No Data Available<p>'); }); function myFunction(){ alert('No data available'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="panel"> <p id="test">onload<p> </div> <input type="submit" class="test_button">

HTML elements, other than the body , do not have an onload event. HTML 元素,除了body ,没有onload事件。 Since adding elements to the DOM is synchronous, you could just invoke myFunction after appending.由于向 DOM 添加元素是同步的,因此您可以在添加后调用myFunction If you want the text to be changed before the alert, you can just use setTimeout .如果您希望在警报之前更改文本,您可以使用setTimeout

 $('.test_button').click(function(){ $('#test').remove(); $('.panel').append('<p id="test"">No Data Available<p>'); setTimeout(myFunction); }); function myFunction(){ alert('No data available'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="panel"> <p id="test">onload<p> </div> <input type="submit" class="test_button">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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