<!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)
{
var link=document.createElement("a");
link.setAttribute("href","url");
link.innerHTML=text;
main.appendChild(link);
}
// 调用函数创建链接
createa("http://www.imooc.com","慕课网");
</script>
</body>
</html>
<!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)
{
var link=document.createElement("a");
link.setAttribute("href",url);
link.innerHTML=text;
main.appendChild(link);
}
// 调用函数创建链接
createa("http://www.imooc.com","慕课网");
</script>
</body>
</html>
求解释
"url"可以变相理解为是字符串,字符串的内容就是url;而不加引号的url可以变相理解为是一个变量名,那变量名可以对应的内容就不绝对了,你可以是一个网址,也可以是字符串
link.setAttribute("href","url");把"url"双引号去掉