<!DOCTYPE html> <html> <body> <h1>Element 对象</h1> <h2>setAttribute() 方法</h2> <a id="myA" href="https://www.sou-xun.com" target="_blank">souxun.com</a>. <p>单击“更改”将链接目标更改为“_self_”。</p> <button onclick="myFunction()">更改</button> <p>请试着单击“更改”前后的链接。</p> <script> function myFunction() { const element = document.getElementById("myA"); if (element.hasAttribute("target")) { element.setAttribute("target", "_self"); } } </script> </body> </html>