HTML DOM setNamedItem() 方法
DOM setNamedItem() 方法使用其在 HTML 文档中的名称向属性节点设置其参数中指定的一个节点。
语法
以下是语法 −
node.setNamedItem(node);
示例
让我们看一个 setNamedItem() 方法的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:50%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
</style>
</head>
<body>
<h1>DOM setNamedItem() method Demo</h1>
<p>Hi! I'm a para element in HTML with some random text</p>
<button onclick="set()" class="btn">Set Attribute using setNamedItem() method</button>
<script>
function set() {
var p=document.querySelector("p");
var attr=document.createAttribute("style");
attr.value="color:#db133a;font-size:1.2rem;";
p.attributes.setNamedItem(attr);
}
</script>
</body>
</html>输出
这将产生以下输出 −
_method.jpg)
单击“使用 setNamedItem() 方法设置属性”按钮来设置元素的属性。
_method_ex.jpg)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP