使用 JavaScript 获取 HTML 元素的属性
本教程指导如何使用 JavaScript 和 jQuery 获取 HTML 元素的属性。
我们将使用带有 querySelector()
和 getElementById()
方法的 getAttribute()
函数,以及 attributes
节点列表来获取属性名称及其值。
getAttribute()
方法输出元素属性的值;该元素可以通过标签或 ID 选择。
我们使用 querySelector
选择标签元素;它给出了文档中与选择器匹配的第一个元素,而 getElementById()
获取具有指定 id 的第一个元素。
attributes
属性返回特定 HTML 元素的属性集合。
在 JavaScript 中使用 getAttribute()
获取 HTML 元素的属性
让我们从 HTML 文档对象模型 (DOM) 的 getAttribute()
函数开始获取指定 HTML 元素的属性值。
示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Get Attribues of HTML Element Using JavaScript</title>
</head>
<body>
<span id="getAttr" name="attr" message="get attributes"></span>
<p id="testP" name="testing"> This is a paragraph </p>
<script>
const getElementByID = document.getElementById("getAttr")
const getElementByQuery = document.querySelector('p');
console.log(getElementByID.getAttribute("name"));
console.log(getElementByQuery.getAttribute("id"));
</script>
</body>
</html>
输出:
"attr"
"testP"
在 JavaScript 中使用 attributes
属性获取 HTML 元素的属性
在下面的代码中,我们使用 attributes
属性来访问特定 HTML 元素的属性名称和值,并使用 push()
方法将它们插入到两个单独的数组中。
你可以在此处找到有关 push()
函数的更多信息。看下面的启动代码来练习。
<!DOCTYPE html>
<html>
<head>
<title>Get Attribues of HTML Element Using JavaScript</title>
</head>
<body>
<span id="getAttr" name="attr" message="get attributes"></span>
<script>
const getElementByID = document.getElementById("getAttr");
attrs = getElementByID.attributes;
n = attrs.length;
attrNameArray = [];
attrValueArray = [];
for (var i = 0; i < n; i++){
attrNameArray.push(attrs[i].nodeName);
attrValueArray.push(attrs[i].nodeValue);
}
console.log("Print attribute names and values with loop");
console.log(attrNameArray);
console.log(attrValueArray);
</script>
</body>
</html>
输出:
"Print attribute names and values with loop"
["id", "name", "message"]
["getAttr", "attr", "get attributes"]
在 JavaScript 中使用 jQuery 获取 HTML 元素的属性
以下代码使用 jQuery 使用 each()
函数获取属性的名称和值。each()
方法为每个匹配的元素运行一个函数/方法。
示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Get Attribues of HTML Element Using JavaScript</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
</head>
<body>
<span id="getAttr" name="attr" message="get attributes"></span>
<script>
var element = $("#getAttr");
$(element[0].attributes).each(function() {
console.log(this.nodeName+':'+this.nodeValue);
});
</script>
</body>
</html>
输出:
"id:getAttr"
"name:attr"
"message:get attributes"
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。