var main = document.body;//创建链接function createa(url,text){ var a = document.createElement("a"); a.setAttribute("href",url); a.setAttribute("a.innerHTML","text"); a.setAttribute("a.style.color","red"); main.appendChild(a);}// 调用函数创建链接createa("http://www.imooc.com","慕课网");
1 回答
咕咕问
TA贡献78条经验 获得超12个赞
a.setAttribute("a.innerHTML","text");
a.setAttribute("a.style.color","red");
setAttribute 前面是属性名 后面是属性值
innerHTML 被解析为属性名了
style="color:red;"
<!DOCTYPE html>
<html>
<body>
<script>
var main = document.body;
//创建链接
function createa(url,text)
{
var a = document.createElement("a");
a.setAttribute("href",url);
a.innerHTML = text;
a.setAttribute("style","color:red;");
main.appendChild(a);
}
// 调用函数创建链接
createa("http://www.imooc.com","慕课网");
</script>
</body>
</html>
添加回答
举报
0/150
提交
取消