如何使用javascript获取具有id的其他div内的div的h2标签的值
我有一个带有 id 的 div,其中还有一些其他没有 id 的 div。
像这样的事情:
<div class="mainDivClass" id="mainDiv">
<div class="subDivClass">
<h2>one</h2>
Hello One!!
</div>
<div class="subDivClass">
<h2>two</h2>
Hello Two!!
</div>
<div class="subDivClass">
<h2>three</h2>
Hello Three!!
</div>
</div>
在我的 javascript 中,我循环遍历上面的 div,例如:
var divLists = document.getElementById('mainDiv').firstChild.childNodes;
for (var i = 0; i < tabLists.length; i++) {
var anchor = divLists[i].firstChild;
var iconFile;
if(i==0)
{
iconFile = 'details.png';
}
else
{
iconFile = 'search1.png';
}
anchor.style.backgroundImage = 'url(' + iconFile + ')';
anchor.style.backgroundRepeat = 'no-repeat';
anchor.style.backgroundPosition = '1px 2px';
anchor.className = 'toplevel-tab';
}
如图所示,我在 i 的值上设置 iconFile 变量。因此,对于 i = 0,它将是details.png,而对于所有其他情况,它将是search1.png。
现在,我想根据元素的 h2 值来决定 iconFile 变量值。 也就是说,如果h2是banana,banana.png将进入iconFile,但如果h2是orange,orange.png将被选择。
如何在javascript中获取h2值?
感谢您的阅读!
尼克
I have a div with id, which has some other div's without id.
Some thing like:
<div class="mainDivClass" id="mainDiv">
<div class="subDivClass">
<h2>one</h2>
Hello One!!
</div>
<div class="subDivClass">
<h2>two</h2>
Hello Two!!
</div>
<div class="subDivClass">
<h2>three</h2>
Hello Three!!
</div>
</div>
In my javascript, I am looping through above div like:
var divLists = document.getElementById('mainDiv').firstChild.childNodes;
for (var i = 0; i < tabLists.length; i++) {
var anchor = divLists[i].firstChild;
var iconFile;
if(i==0)
{
iconFile = 'details.png';
}
else
{
iconFile = 'search1.png';
}
anchor.style.backgroundImage = 'url(' + iconFile + ')';
anchor.style.backgroundRepeat = 'no-repeat';
anchor.style.backgroundPosition = '1px 2px';
anchor.className = 'toplevel-tab';
}
As shown, I am setting iconFile variable on value of i. So for i = 0, it would be details.png while for all others, it would be search1.png.
Now, I want to decide the iconFile variable value based on the h2 value of the element.
That is, if h2 is banana, banana.png will go in iconFile but if h2 is orange, orange.png will be selected.
How to get h2 value inside javascript ?
Thanks for reading!!
Nik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用innerHTML,它是一种不可靠的微软专有方法;如果您习惯使用它,如果您在应用程序级别开始编码并且无法找出原因,您将立即开始遇到问题。坚持使用 DOM 规范。
显然可以将其放入循环中的示例...
.parentNode - 当前引用元素的父元素。
.parentNode.parentNode.parentNode - 您可以根据需要在 DOM 上或周围使用它。
.childNodes[0] - 子元素的索引,不包含对元素之后的文本节点的引用(为此使用 Treewalker)。
.nodeValue - 节点的文本值,不要使用innerHTML。
.textContent - 获取或设置元素的文本(但没有子元素);比
nodeValue
更容易一些,尽管它仍然有合理的限制。.previousSibling - 引用元素之前的元素,而不是子元素/父元素。
.nextSibling - 引用元素之后的元素,而不是子/父元素。
您可以使用 in 运算符显示任何对象的所有对象(例如方法、属性和其他对象),以发现您还可以使用哪些内容...
应该注意的是,如果您无法使用 HTML 解析器
。 nodeName
将全部大写(例如旧的 Internet Explorer 方式),与使用 XML 解析器 (application/xhtml+xml) 相比,.nodeName
将正确返回小写的元素名称(除非您真的是90年代了风格什么的)。还应该注意的是,当您使用
previousSibling
和nextSibling
时,仅换行符就会创建一个textNode
并且这些换行符会与 CSS 混淆(将font-size
设置为 5px 通常可以消除这种情况)。Don't use innerHTML, it's an unreliable proprietary Microsoft method; should you get used to using it you will immediately begin having problems if you start coding at an application level and not be able to figure out why. Stick to using DOM specifications instead.
An example that you can obviously throw in to a loop...
.parentNode - The parent element of the currently referenced element.
.parentNode.parentNode.parentNode - You can use this as much as you want to go up or around the DOM.
.childNodes[0] - Index of child elements, does NOT contain reference to text nodes AFTER an element (use treewalker for that).
.nodeValue - The text value of a node, do NOT use innerHTML.
.textContent - Gets or sets the text of an element (but no child elements); a bit easier than
nodeValue
though it still has reasonable limitations..previousSibling - The element BEFORE the reference element, not a child/parent.
.nextSibling - The element AFTER the reference element, not a child/parent.
You can reveal all objects (e.g. methods, properties and other objects) for any object using the in operator to discover what else is available to you...
It should be noted that if you're stuck using the HTML parser
.nodeName
will be all uppercase (e.g. the old Internet Explorer way) versus using the XML parser (application/xhtml+xml) the.nodeName
will properly return the element's name as lowercase (unless you're really in to the 90's style or something).It should also be noted that when you use
previousSibling
andnextSibling
that line breaks alone will create atextNode
and those line breaks will mess with CSS (setting thefont-size
to 5px will generally eliminate this).如果您想要 mainDivClass 中的所有 H2 元素,您可以使用 getElementsByTagName 方法:
这将所有 H2 元素作为元素数组返回。
If you want all the H2 elements inside the mainDivClass you can use the getElementsByTagName method:
This returns all the H2 elements as an array of elements.
h2 值将按如下方式使用:
The h2 value will be used as below: