javascript问题求解

来源:百度知道 编辑:UC知道 时间:2024/06/29 21:13:53
今天刚学这个课程
编写一个脚本,该脚本保存在外部文件“test.js”中,当页面载入时在页面上直接显示标题一格式的红色文本“Hello World!”。

问题是 test.js 里面的文件要怎么设置..

window.onload=function(){
document.write("<h1 style='color:red;'>Hello World!</h1>");
}

window.onload=function(){}

window.onload = function (){
try{
var tempNode = document.createElement('h1');
tempNode.setAttribute('align' , 'center');
tempNode.style.color = 'red';

var textNode = document.createTextNode('this is the header');

tempNode.appendChild(textNode);

var body = document.getElementsByTagName('body')[0];

if(body.childNodes.length > 0 ){
body.insertBefore(tempNode,body.firstChild);
}else{

body.appendChild(tempNode);
}
}catch(e){

}

}

document.getElementById("id名").innerHTML="<font color='red'>Hello World"</font>"