代码示例:(标识:jsck_element_hasattribute_2)
<!DOCTYPE html>
<html>
<body>

<h1>Element 对象</h1>

<h2>setAttribute() 方法</h2>

<a id="myA" href="https://www.sou-xun.com" target="_blank">编程教程</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>
运行结果: