参考一个div里面的div,与另一个里面的div相同 [英] Referring to a div inside a div with the same ID as another inside another

查看:116
本文介绍了参考一个div里面的div,与另一个里面的div相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果与id相同,那么我可以通过id引用嵌套的div。

How can I refer to a nested div by id when it has the same id as a div nested in a similarly named div

例如

<div id="obj1">
    <div id="Meta">
        <meta></meta>
    </div>
</div>

<div id="obj2">
    <div id="Meta">
        <meta></meta>
    </div>
</div>

我想获得元的innerHTML

I want to get the innerHTML of meta

document.getElementById('obj1').getElementById('Meta').getElementsByTagName('meta')

不工作

推荐答案

ID只能在有一个该页面上的项目,无论是SPAN,DIV还是其他。 CLASS是您可能有重复元素时应该使用的。

IDs should only be used when there is one of that item on the page, be it a SPAN, DIV or whatever. CLASS is what you should use for when you may have a repeating element.

代码不起作用,因为您使用唯一ID指向元素,但是多于一页。

Code there doesn't work because you're referring to an element by unique ID, but have more than one on the page.

这篇关于参考一个div里面的div,与另一个里面的div相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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