请大家帮我看以,谢谢

来源:9-16 创建元素节点createElement

marshall_stan

2015-08-04 13:18

<!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 btn = document.createElement("a");
    btn.setAttribute("href",url);
    btn.setAttribute("value",text);
    //btn.setAttribute("color","red");
    btn.style.color="red";
    main.appendChild(btn);
}
// 调用函数创建链接
createa("http://www.imooc.com","慕课网");

</script> 
</body>
</html>

这代码哪里错了呢?谢谢?

另外,用setAttribute怎么设置颜色属性啊?

写回答 关注

2回答

  • 伊兮尘昔
    2015-08-05 14:27:52
    已采纳

    关键是没在页面上显示你写的这个链接, btn.setAttribute("value",text);这块不能这么写,应该用innerHTML来做。你是要设置背景色?还是设置字体颜色?

    marsha...

    谢谢啦!

    2015-08-05 15:04:08

    共 1 条回复 >

  • 伊兮尘昔
    2015-08-05 14:30:44

    颜色设置的话要写 btn.setAttribute("style","color:red");

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

467374 学习 · 21877 问题

查看课程

相似问题