<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var main = document.body;
//创建链接
function createa(url,text)
{
// 链接地址 http://www.imooc.com,文本为:慕课网
alert("000");
var a=createElement("a");
a.addAttribute("href",url);
a.innerHTML=text;
document.innerHTML=a;
};
// 调用函数创建链接
createa("http://www.imooc.com","慕课网");
</script>
</body>
</html>
给你个参考
function createa(url,text)
{
//var body =document.body;
var a=document.createElement("a");
a.href=url;
a.innerHTML=text;
a.style.color="red";
main.appendChild(a);
/*
var linktomk=document.createElement("a");
linktomk.setAttribute("href",url);
linktomk.innerHTML=text;
main.appendChild(linktomk);*/
}
// 调用函数创建链接
createa(" https://www.imooc.com/","慕课网");
var main = document.body;
function createa(url, text) {
var a = document.createElement("a"); // 你这里 createElement 方法是document对象下的,要加document
a.setAttribute("href", url); //添加属性的代码是是setAttribute不是addAttribute
a.innerHTML = text;
main.appendChild(a); //添加节点的代码是 main.appendChild(a) 不是 document.innerHTML=a
}
createa("http://www.imooc.com", "慕课网");