DIV CSS 佈局教程網

JavaScript動態創建鏈接的方法
編輯:JavaScript綜合知識     

本文分享JavaScript動態創建鏈接的方法,供有需要的朋友參考。具體分析如下:

動態創建鏈接示例:

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>動態添加鏈接</title>
  <script type="text/javascript">
  function AppendLink() {
    var div = document.getElementById("divMain");
    var linkTmp = document.createElement("a");
    linkTmp.href = "https://www.baidu.com";
    linkTmp.innerText = "百度"; // 鏈接要使用innerText,不能使用value
    div.appendChild(linkTmp);
  }
  </script>
</head>
<body>
  <div id="divMain"></div>
  <input type="button" value="添加鏈接" onclick="AppendLink()" />
</body>
</html>

希望本文所述對大家的JavaScript程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved