如何确定< script>的位置?标签里面的标签? [英] how can you determine location of <script> tag from inside said tag?

查看:73
本文介绍了如何确定< script>的位置?标签里面的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚当前javascript正在运行的脚本标记的位置。真正发生的是我需要从里面确定一个src'd,动态插入的javascript文件在DOM中的位置。这些是动态生成的标签;代码段:

I am trying to figure out the location of the script tag the current javascript is running in. What is really going on is that I need to determine from inside a src'd, dynamically inserted javascript file where it is located in the DOM. These are dynamically generated tags; code snippet:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>where am i?</title>
  <script type="text/javascript" charset="utf-8">
    function byId(id) {
      return document.getElementById(id);
    }

    function create_script(el, code) {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.text = code;
      el.appendChild(script);
    }
  </script>
</head>
<body>
  <div id="find_me_please"></div>
  <script>
    create_script(byId("find_me_please"), "alert('where is this code located?');");
  </script>
</body>
</html>


推荐答案

你可以给脚本一个id标签,像这样的家伙确实...... ...

You could give the script an id tag, like this dude does...

这篇关于如何确定&lt; script&gt;的位置?标签里面的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆