javascript 获取对 <script> 父元素的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10299846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:22:50  来源:igfitidea点击:

Obtain a reference to <script> parent element

javascriptjqueryreference

提问by diosney

I have a similar problem like posted herebut instead of obtain the parent id, how Can I get a reference to the script parent without having to code an id for the script tag?

我有一个类似的问题,比如在这里发布但不是获取父 id,我如何获得对脚本父的引用,而不必为脚本标记编码 id?

For example, I have the following code injected to a website:

例如,我将以下代码注入网站:

<div>
     some other html tags
     <script src="http://path_to_my_script.js"></script>
</div>

What I need is that in my path_to_my_script.jsfile I can get a reference to the outer div.

我需要的是在我的path_to_my_script.js文件中我可以获得对外部div.

There is a catch and is that the code will be copied&pasted in several places in the same webpage, which makes an identifier useless.

有一个问题,代码将被复制并粘贴到同一网页的多个位置,这使得标识符无用。

There is any chance to do that without to code an id in the entire code?

有没有机会做到这一点而无需在整个代码中编码一个 id ?

If the solution is with jQuery the better :D

如果解决方案是使用 jQuery 更好:D

Thanks in advance.

提前致谢。

回答by Rob W

As mentioned in What is the current element in Javascript?, the current <script>element is also the last <script>element, at the time of execution.

正如JavaScript 中的当前元素是什么?,目前的<script>元素也是最后一个<script>元素,在执行的时间

The answer also applies to your case, since the load of the external script is not deferred (through the asyncor deferattribute).

答案也适用于您的情况,因为外部脚本的加载不会延迟(通过asyncordefer属性)。

var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1];
var parentTag = scriptTag.parentNode;

Most browsers (even IE6) support document.scripts. Firefox supports this object as of version 9. So, the following code can also be used:

大多数浏览器(甚至 IE6)都支持document.scripts. Firefox 从版本 9 开始支持此对象。因此,也可以使用以下代码:

var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.parentNode;

回答by Aaron

You can use the attribute selector to get a script with a specific src, then parent('div')to jump to its containing divelement.

您可以使用属性选择器获取具有特定 的脚本src,然后parent('div')跳转到其包含div元素。

var parentDiv = $('script[src="path/to/script.js"]').parent('div');

回答by Alex Turpin

You need a way to refer your script by. How else do you expect to know which script you want? If you only have one script, you could always do

您需要一种方法来引用您的脚本。您还希望如何知道您想要哪个脚本?如果你只有一个脚本,你总是可以做的

var myScript = document.getElementsByTagName("script")[0];

to get a DOM element of the first script on your page. Or you could use jQuery and loop through your script to do something with each:

获取页面上第一个脚本的 DOM 元素。或者您可以使用 jQuery 并循环遍历您的脚本来对每个脚本执行某些操作:

$("script").each(function() {
    alert($(this).html());
});