Home  >  Article  >  Web Front-end  >  How to dynamically create links using js? Tips for dynamically manipulating page elements using js

How to dynamically create links using js? Tips for dynamically manipulating page elements using js

零下一度
零下一度Original
2017-04-26 10:07:151672browse

This article mainly introduces the method of dynamically creating links with javascript, involving the techniques of dynamically operating page elements with javascript. Friends in need can refer to the following

The example of this article describes the method of dynamically creating links with javascript. Share it with everyone for your reference. The specific analysis is as follows:

Example of dynamically creating links:


<html xmlns="http://www.php.cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>动态添加链接</title>
 <script type="text/javascript">
 function AppendLink() {
  var p = document.getElementById("pMain");
  var linkTmp = document.createElement("a");
  linkTmp.href = "http://www.baidu.com";
  linkTmp.innerText = "百度"; 
  //链接要使用innertText,不能使用value
  p.appendChild(linkTmp);
 }
 </script>
</head>
<body>
 <p id="pMain"></p>
 <input type="button" value="添加链接" onclick="AppendLink()" />
</body>
</html>

The above is the detailed content of How to dynamically create links using js? Tips for dynamically manipulating page elements using js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn