How to add onload event to a div element

前端 未结 24 2253
谎友^
谎友^ 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:49

    When you load some html from server and insert it into DOM tree you can use DOMSubtreeModified however it is deprecated - so you can use MutationObserver or just detect new content inside loadElement function directly so you will don't need to wait for DOM events

    var ignoreFirst=0;
    var observer = (new MutationObserver((m, ob)=>
    {
      if(ignoreFirst++>0) {
        console.log('Element add on', new Date());
      }
    }
    )).observe(content, {childList: true, subtree:true });
    
    
    // simulate element loading
    var tmp=1;
    function loadElement(name) {  
      setTimeout(()=>{
        console.log(`Element ${name} loaded`)
        content.innerHTML += `<div>My name is ${name}</div>`; 
      },1500*tmp++)
    }; 
    
    loadElement('Michael');
    loadElement('Madonna');
    loadElement('Shakira');
    <div id="content"><div>

    0 讨论(0)
  • 2020-11-22 00:52

    we can use MutationObserver to solve the problem in efficient way adding a sample code below

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <style>
            #second{
                position: absolute;
                width: 100px;
                height: 100px;
                background-color: #a1a1a1;
            }
        </style>
    </head>
    <body>
    <div id="first"></div>
    <script>
        var callthis = function(element){
               element.setAttribute("tabIndex",0);
            element.focus();
            element.onkeydown = handler;
            function handler(){
                    alert("called")
            }
        }
    
    
        var observer = new WebKitMutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                for (var i = 0; i < mutation.addedNodes.length; i++)
                if(mutation.addedNodes[i].id === "second"){
                    callthis(mutation.addedNodes[i]);
                }
    
            })
        });
        observer.observe(document.getElementById("first"), { childList: true });
    
    
        var ele = document.createElement('div');
        ele.id = "second"
    
        document.getElementById("first").appendChild(ele);
    
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 00:53

    I had the same question and was trying to get a Div to load a scroll script, using onload or load. The problem I found was that it would always work before the Div could open, not during or after, so it wouldn't really work.

    Then I came up with this as a work around.

    <body>
    
    <span onmouseover="window.scrollTo(0, document.body.scrollHeight);" 
    onmouseout="window.scrollTo(0, document.body.scrollHeight);">
    
    <div id="">
    </div>
    
    <a href="" onclick="window.scrollTo(0, document.body.scrollHeight);">Link to open Div</a>
    
    </span>
    </body>
    

    I placed the Div inside a Span and gave the Span two events, a mouseover and a mouseout. Then below that Div, I placed a link to open the Div, and gave that link an event for onclick. All events the exact same, to make the page scroll down to bottom of page. Now when the button to open the Div is clicked, the page will jump down part way, and the Div will open above the button, causing the mouseover and mouseout events to help push the scroll down script. Then any movement of the mouse at that point will push the script one last time.

    0 讨论(0)
  • 2020-11-22 00:54

    I really like the YUI3 library for this sort of thing.

    <div id="mydiv"> ... </div>
    
    <script>
    YUI().use('node-base', function(Y) {
      Y.on("available", someFunction, '#mydiv')
    })
    

    See: http://developer.yahoo.com/yui/3/event/#onavailable

    0 讨论(0)
  • 2020-11-22 00:55

    Try this! And never use trigger twice on div!

    You can define function to call before the div tag.

    $(function(){
        $('div[onload]').trigger('onload');
    });
    

    DEMO: jsfiddle

    0 讨论(0)
  • 2020-11-22 00:55

    You cannot add event onload on div, but you can add onkeydown and trigger onkeydown event on document load

    $(function ()
    {
      $(".ccsdvCotentPS").trigger("onkeydown");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    
    <div  onkeydown="setCss( );"> </div>`

    0 讨论(0)
提交回复
热议问题