javascript 获取第二个javascript的父div id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19055939/
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-27 14:12:34  来源:igfitidea点击:

Get parent div id of second javascript?

javascriptjqueryhtml

提问by realtek

I have a div which is like this:

我有一个 div 是这样的:

<div id="mainDiv">
<script>
javascript here...
</script>
</div>

I basically want to get the ID of the div it is inside.

我基本上想得到它里面的 div 的 ID。

I have tried using jQuery and classic javascript to do this but it keeps returning undefined.

我曾尝试使用 jQuery 和经典的 javascript 来做到这一点,但它一直返回未定义。

Does anyone have any idea's? Thanks

有没有人有任何想法?谢谢

回答by Karl-André Gagnon

Since broswer read the code from up to down, you can do this:

由于broswer是从上往下读取代码,你可以这样做:

Vanilla JS

香草JS

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

jQuery

jQuery

var id = $('script').last().parent().prop('id');

when it will read this code, the last script tag is the one the browser is reading.

当它将读取此代码时,最后一个脚本标记是浏览器正在读取的标记。

Here's a fiddle : http://jsfiddle.net/nb234/

这是一个小提琴:http: //jsfiddle.net/nb234/

回答by Ritesh Kumar

Check this out http://jsfiddle.net/78N9A/1/

看看这个http://jsfiddle.net/78N9A/1/

<div id="parent">
   <script id="scr">
       function show()
       {
         alert('hello');
       }
    </script>
</div>

window.onload = function(){
    var ele = document.getElementById('scr').parentNode;
    alert(ele.id);

}

回答by andi

If I understand correctly, you are saying that the id on the parent div is unknown in advance, and you want to use JS to determine what it is.

如果我理解正确,你是说父div上的id是事先未知的,你想用JS来确定它是什么。

If you can't edit the script tag to add an id, maybe you could do a document.write()within that block to add in a little hidden element, then you can test to see what that element's parent is.

如果您无法编辑脚本标记来添加 id,也许您可​​以document.write()在该块中执行一个操作来添加一个隐藏的小元素,然后您可以测试以查看该元素的父元素是什么。

http://jsfiddle.net/AtRYF/

http://jsfiddle.net/AtRYF/

回答by andi

<script id="script">

Then

然后

$('#script').parent();

回答by Timotheus Triebl

jQuery(document).ready(function($) {    

    $('div').attr('id');

});

should get you the value you need.

应该让你得到你需要的价值。

回答by Shawn Whinnery

JAVASCRIPT: Give your script an ID and then use parentNode to move up the DOM.

JAVASCRIPT:给你的脚本一个 ID,然后使用 parentNode 向上移动 DOM。

var the_div_you_want = document.getElementById("skript").parentNode.id;

JQUERY: Pretty sure .parent() is what you're after

JQUERY:很确定 .parent() 是你所追求的

$('#scriptId').parent().attr("id");

Regardless, you need to find some way to locate the script and the move up the dom to find that relationship. Hope this helps.

无论如何,您需要找到某种方法来定位脚本并向上移动 dom 以找到该关系。希望这可以帮助。

http://jsfiddle.net/Zbuf8/1/

http://jsfiddle.net/Zbuf8/1/