javascript 动态创建链接

来源:互联网 发布:java简单源代码 编辑:程序博客网 时间:2024/05/22 13:56
举例,动态创建链接
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>动态添加链接</title>
    <script type="text/javascript">
        function AppendLink() {
            var div = document.getElementById("divMain");
            var linkTmp = document.createElement("a");
            linkTmp.href = "http://www.baidu.com";
            linkTmp.innerText = "百度";            //链接要使用innertText,不能使用value
            div.appendChild(linkTmp);
        }
    </script>
</head>
<body>
    <div id="divMain"></div>
    <input type="button" value="添加链接" onclick="AppendLink()" />
</body>
</html>
原创粉丝点击