如何使用JS获取外部div id? [英] how to use JS to get an outer div id?

查看:98
本文介绍了如何使用JS获取外部div id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个这种形式的HTML块:


< div id =" outer0" class =" outerDiv">

< div class =" innerDiv">

< table height =" 100%" id =" table0">

< tr style =" background-color:#e0e0e0;高度:15px;">< td

align =" right">< a href =" javascript :closeMe(''/ * Javascript到这里

* /'');">点击我< / a>< / td>< / tr>

<! - 那里可能是更多的表格行 - >

< / table>

< / div>

< / div>


在你看到/ * Javascript到这里* /的地方,我想

来放置一个表达式,其值为:最外层的DIV,

所以在这种情况下,它会评估为outer0。有没有人知道一个

的跨浏览器JS表达式会这样做?


谢谢, - Dave

解决方案

< la *********** @ zipmail.comwrote in message

news:11 ****** ***************@s48g2000cws.googlegro ups.com ...




我有一个这种形式的HTML块:


< div id =" outer0" class =" outerDiv">

< div class =" innerDiv">

< table height =" 100%" id =" table0">

< tr style =" background-color:#e0e0e0;高度:15px;">< td

align =" right">< a href =" javascript :closeMe(''/ * Javascript到这里

* /'');">点击我< / a>< / td>< / tr>

<! - 那里可能是更多的表格行 - >

< / table>

< / div>

< / div>


在你看到/ * Javascript到这里* /的地方,我想

来放置一个表达式,其值为:最外层的DIV,

所以在这种情况下,它会评估为outer0。有没有人知道一个

跨浏览器的JS表达式会做到这一点?



document.getElementsByTagName(''div'')[0]


应该这样做。


-Lost


-Lost在2007年1月30日下午7:22说出以下内容:


< la *********** @ zipmail.comwrote in message

news:11 ************ *********@s48g2000cws.googlegro ups.com ...


>

我有一个此格式的HTML块:

< div id =" outer0" class =" outerDiv">
< div class =" innerDiv">
< table height =" 100%" id =" table0">
< tr style =" background-color:#e0e0e0;高度:15px;">< td
align =" right">< a href =" javascript :closeMe(''/ * Javascript在这里
* /'');">点击我< / a>< / td>< / tr>
<! - 可能有更多的表格行 - >
< / table>
< / div>
< / div>

在您看到/ * Javascript go here * /的地方,我想
将表达式计算为最外层DIV的id,所以在这种情况下,它将评估为outer0。有谁知道这样做的跨浏览器JS表达式?



document.getElementsByTagName(''div'')[0]


应该这样做。



它不会。考虑:


< div id =" outerDivYouGet">< / div>

< div id =" outerDivWanted">

< div id =" innerDiv">你还认为

document.getElementsByTagName(''div'')[0]

仍然指的是这个元素的最外层div元素

?提示:它没有。

< / div>

-

兰迪

Chance Favors准备好的心灵

comp.lang.javascript常见问题 - http: //jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


1月31日上午10点22分,-Lost ; < spam_ninjaREMOV ... @ REMOVEMEcomcast.net>

写道:


< laredotorn ... @ zipmail.comwrote in message


新闻:11 ********************* @ s48g2000cws.googlegro ups.com ......
< blockquote class =post_quotes>


我有一个这种形式的HTML块:


< div id =" outer0" class =" outerDiv">

< div class =" innerDiv">

< table height =" 100%" id =" table0">

< tr style =" background-color:#e0e0e0;高度:15px;">< td

align =" right">< a href =" javascript :closeMe(''/ * Javascript到这里

* /'');">点击我< / a>< / td>< / tr>

<! - 那里可能是更多的表格行 - >

< / table>

< / div>

< / div>


在你看到/ * Javascript到这里* /的地方,我想要

到将表达式计算为最外层DIV的id,

所以在这种情况下,它将计算为outer0。有没有人知道一个

跨浏览器的JS表达式会做到这一点?



document.getElementsByTagName(''div'')[0]


应该这样做。



仅当最外面的div是文档中的第一个时。考虑:


< div ...< / div>

< div>

< div>什么是我最外面的div?< / div>

< / div>

以下函数爬上DOM并返回对
$ b $的引用b遇到的最后一个div。如果没有封闭的div,则返回

undefined。


function getOutermostDiv(el){

var outermostDiv;

while(el.parentNode&&(el = el.parentNode)){

if(el.tagName&& el.tagName.toLowerCase()== ''div''){

outermostDiv = el;

}

}

返回outermostDiv;

}

并且测试:


< div id =" blah">

< div id =" blah00">

< button onclick ="

var x = getOutermostDiv(this);

if(x){

(x.id)? alert(x.id):alert(''no id'');

} else {

alert(''no outer div'');

}

">显示最外面的DIV ID< / button>

< / div>

< ; / div>


-

Rob


Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv">
<div class="innerDiv">
<table height="100%" id="table0">
<tr style="background-color:#e0e0e0; height:15px;"><td
align="right"><a href="javascript:closeMe(''/* Javascript goes here
*/'');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

Thanks, - Dave

解决方案

<la***********@zipmail.comwrote in message
news:11*********************@s48g2000cws.googlegro ups.com...

Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv">
<div class="innerDiv">
<table height="100%" id="table0">
<tr style="background-color:#e0e0e0; height:15px;"><td
align="right"><a href="javascript:closeMe(''/* Javascript goes here
*/'');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?

document.getElementsByTagName(''div'')[0]

That should do it.

-Lost


-Lost said the following on 1/30/2007 7:22 PM:

<la***********@zipmail.comwrote in message
news:11*********************@s48g2000cws.googlegro ups.com...

>Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv">
<div class="innerDiv">
<table height="100%" id="table0">
<tr style="background-color:#e0e0e0; height:15px;"><td
align="right"><a href="javascript:closeMe(''/* Javascript goes here
*/'');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?


document.getElementsByTagName(''div'')[0]

That should do it.

It won''t. Consider:

<div id="outerDivYouGet"></div>
<div id="outerDivWanted">
<div id="innerDiv">Do you still think that
document.getElementsByTagName(''div'')[0]
still refers to the outer most div element
of this element? Hint: It doesn''t.
</div>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


On Jan 31, 10:22 am, "-Lost" <spam_ninjaREMOV...@REMOVEMEcomcast.net>
wrote:

<laredotorn...@zipmail.comwrote in message

news:11*********************@s48g2000cws.googlegro ups.com...

Hi,

I have a block of HTML of this form:

<div id="outer0" class="outerDiv">
<div class="innerDiv">
<table height="100%" id="table0">
<tr style="background-color:#e0e0e0; height:15px;"><td
align="right"><a href="javascript:closeMe(''/* Javascript goes here
*/'');">Click Me</a></td></tr>
<!-- There may be more table rows -->
</table>
</div>
</div>

In the place where you see "/* Javascript goes here */", I would like
to put an expression that evaluates to the id of the outer most DIV,
so in this case, it would evaluate to "outer0". Does anyone know a
cross-browser JS expression that will do this?


document.getElementsByTagName(''div'')[0]

That should do it.

Only if the outermost div is the first in the document. Consider:

<div... </div>
<div>
<div>What is my outermost div?</div>
</div>
The following function climbs up the DOM and returns a reference to
the last div encountered. If there is no enclosing div, it returns
undefined.

function getOutermostDiv(el) {
var outermostDiv;
while (el.parentNode && (el = el.parentNode)) {
if (el.tagName && el.tagName.toLowerCase() == ''div''){
outermostDiv = el;
}
}
return outermostDiv;
}
And a test:

<div id="blah">
<div id="blah00">
<button onclick="
var x = getOutermostDiv(this);
if (x) {
(x.id)? alert(x.id):alert(''no id'');
} else {
alert(''no outer div'');
}
">Show outer-most DIV id</button>
</div>
</div>

--
Rob


这篇关于如何使用JS获取外部div id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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