作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="pagehead">
<h1>Sample 1</h1>
<h1>Sample 2</h1>
<p>Thank you</p>
</div>
<button onclick="savetoJSON()">Save</button>
</body>
<script>
var count=$("#pagehead").children().length;
for(i=0;i<count;i++)
{
var ele=$("#pagehead").children()[i];
}
</script>
</html>
我想以字符串格式一一获取所有子级的标记名。我尝试了 .prop("tagName");
,但没有效果。除此之外,我想知道如何将 [object HTMLHeadingElement] 对象转换为 string 。
最佳答案
element.tagName
是正确的选择。但请注意,每次想要访问子对象时都遍历 dom 会适得其反:
var children = $("#pagehead").children()
children.each(function(index,el){
console.log(el.tagName);
})
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="pagehead">
<h1>Sample 1</h1>
<h1>Sample 2</h1>
<p>Thank you</p>
</div>
<button onclick="savetoJSON()">Save</button>
</body>
</html>
关于javascript - 如何获取 div 或任何子元素的标记名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999228/
我是一名优秀的程序员,十分优秀!