What is the current element in Javascript? [duplicate]

有些话、适合烂在心里 提交于 2019-11-28 23:43:55

Script are executed in the order of appearance in the document. The contents of a script tag are evaluated on encounter, so, the last <script> element is always the current one.

Code:

<div>
  <script>
    var scriptTag = document.getElementsByTagName('script');
    scriptTag = scriptTag[scriptTag.length - 1];

    var parent = scriptTag.parentNode;
  </script>
</div>

Firefox:

document.currentScript.parentElement

Chrome:

document.scripts.length
$('script#some_id').parent().html('blah blah');

Try this

<div id="mydiv">
 <script type="text/javascript">
  var time = new Date(), hrs = time.getHours(), min = time.getMinutes();
  document.getElementById('mydiv').innerHTML = 'It is '+hrs+":"+(min<10?'0':'')+min;
 </script>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!