gpt4 book ai didi

jquery - 如何将标签内容(例如标签名称和属性)显示为字符串?

转载 作者:行者123 更新时间:2023-12-01 07:33:28 25 4
gpt4 key购买 nike

好吧,所以我想做的是使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com