首页 新闻 会员 周边

这个javaScript页面怎么不显示内容也不提示错误?

0
悬赏园豆:5 [已解决问题] 解决于 2009-01-06 11:46

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>无标题页</title>
    <script type="text/javascript">
    function createA(url,text)
    {
       var o=document.body;
       if(o)
       {
           var a=document.createElement("a");
           a.setAttribute("href",url);
           a.setAttribute("text",text);
           a.style.color="red";
           o.appendChild(a);
       }
       else
       {
          window.alert("0对象为空");
       }
    }
   
    </script> 

</head>
<body onload="createA('http://www.sdut.edu.cn','山东理工大学')">
<div id="main"></div>
</body>
</html>

爱上编程的主页 爱上编程 | 初学一级 | 园豆:55
提问于:2008-12-22 20:04
< >
分享
最佳答案
0

var a=document.createElement("a");
a.href = url;
a.innerHTML = text; //这儿用innerHTML而不是innerText因为有浏览器不支持innerText
a.style.color = 'red';
o.appendChild(a);

你写的代码都是可以执行的,只是没有设对属性.

玉开 | 大侠五级 |园豆:8822 | 2008-12-23 08:55
其他回答(2)
0

把那段脚本放在</html>上面试试,我觉得应该是你的js执行的有些早了

GUO Xingwang | 园豆:3885 (老鸟四级) | 2008-12-22 20:57
0

           var a=document.createElement("a");
           a.setAttribute("href",url);
           //a.setAttribute("text",text);
           a.innerText = text;
           a.style.color="red";
           o.appendChild(a);

BB_Coder | 园豆:797 (小虾三级) | 2008-12-23 08:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册