How to add onload event to a div element

前端 未结 24 2564
谎友^
谎友^ 2020-11-22 00:26

How do you add an onload event to an element?

Can I use:

for t

24条回答
  •  眼角桃花
    2020-11-22 00:40

    As all said, you cannot use onLoad event on a DIV instead but it before body tag.

    but in case you have one footer file and include it in many pages. it's better to check first if the div you want is on that page displayed, so the code doesn't executed in the pages that doesn't contain that DIV to make it load faster and save some time for your application.

    so you will need to give that DIV an ID and do:

    var myElem = document.getElementById('myElementId');
    if (myElem !== null){ put your code here}
    

提交回复
热议问题