运行 ❯
获取你
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>The Document Object</h1> <h2>The createAttribute() and setAttributeNode() Methods</h2> <a id="myAnchor">Go to W3schools.com</a> <p>Click "Add" to add a href attribute to the anchor above.</p> <button onclick="myFunction()">Add</button> <script> function myFunction() { // Create a href attribute: const attr = document.createAttribute("href"); // Set the value of the href attribute: attr.value = "https://w3schools.org.cn"; // Add the href attribute to an element: document.getElementById("myAnchor").setAttributeNode(attr); } </script> </body> </html>