作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,所以我想做的是使用 DOM 自省(introspection)来递归分析 DOM 的当前状态,并将其作为单个格式化字符串返回以在其他地方显示。标准 DOM 函数或 JQuery 足以满足我的目的。到目前为止我已经尝试过类似的事情
var txt = $("body").tagName;
var txt = $("body").get();
var txt = $("body").nodeName;
var txt = new String($("body").tagName);
等等。
每次我尝试其中之一时,它要么返回节点对象本身,要么返回未定义的。如何获取它以便我可以返回诸如 body 或 之类的字符串而不是对象本身?
就此而言,我如何获取以字符串形式返回的属性的名称和值?
最佳答案
您需要使用get(n)
如果您想查看 jQuery 对象的 DOM 属性,则从 jQuery 对象获取原始 DOM 节点,其中 n
是 jQuery 对象中的第 n 个元素。
nodeName
将为您提供大写的节点名称,并且可以使用 getAttribute('attributeName')
或简单的 .attributeName
获取属性的值。
// Contrived example; returns BODY
var n = document.body.nodeName;
// Get the value of an attribute from an element
var n = document.getElementById('id').title;
// Unsupported attribute's value need to be obtained through `getAttribute`
var n = document.getElementById('id').getAttribute('placeholder');
查看此表以获取 DOM 函数和属性的完整列表:http://www.quirksmode.org/dom/w3c_core.html
关于jquery - 如何将标签内容(例如标签名称和属性)显示为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4217684/
我是一名优秀的程序员,十分优秀!