Access element, which contains current [removed]

后端 未结 2 536
失恋的感觉
失恋的感觉 2021-01-25 14:02

I\'ve got following page:

相关标签:
2条回答
  • 2021-01-25 14:27

    You can try this, though it's somewhat of a hack:

    (function() {
      var scr = document.getElementsByTagName('script'),
          parent = scr[scr.length - 1].parentNode;
      // parent is the parent node of the last script on the page
    })();
    

    If you've got code in <script> tags like that, then when it runs the last script on the page will be the one that contained it.

    0 讨论(0)
  • 2021-01-25 14:42

    I remember this from a previous question. The number of scripts on the page is incremented by 1 with each script that is processed, and they are processed in order. So this function will get the current script number:

    function countScripts() {
        return document.scripts.length;
    }
    

    Then you can go get the parentNode of that script:

    var thisScriptParent = document.scripts[countScripts()].parentNode;
    
    0 讨论(0)
提交回复
热议问题