有个js问题想问一下:
function cleardesc()
{
d = document.getElementById("idnum");
d.innerHTML = "";
}
js中有写:
if(e.target)
{
whichlink = e.target.id;
}
else
{
whichlink = e.srcElement.id;
}
在IE8中会出错,但是在Firefox里面可以正常运行,是因为IE8不支持innerHTML这个属性吗?
谢谢!
function cleardesc()
{
d = document.getElementById("idnum");
d.innerHTML = "";
}
js中有写:
if(e.target)
{
whichlink = e.target.id;
}
else
{
whichlink = e.srcElement.id;
}
在IE8中会出错,但是在Firefox里面可以正常运行,是因为IE8不支持innerHTML这个属性吗?
谢谢!
alert(d); //瞧瞧
有试过了,会弹出一个[object]内容
附上源文件:
js:
function cleardesc()
{
d = document.getElementById("description");
d.innerHTML = "Welcome to Js word!";
}function hover(e)
{
var desc;
if (!e) var e = window.event;
//which link was the mouse over?
if(e.target)
{
whichlink = e.target.id;
}
else
{
whichlink = e.srcElement.id;
}
//choose the appropriate description
switch(whichlink)
{
case "order":
desc = "Order a product";
break;
case "email":
desc = "Send us a message";
break;
case "complain":
desc = "Insult us, our products,or our families";
break;
default:
desc = "Error!";
break;
}
//display the description in the H2
d = document.getElementById("description");
d.innerHTML = desc;
}
//set up the event handlers
orderlink = document.getElementById("order");
orderlink.onmouseover = hover;
orderlink.onmouseout = cleardesc;
emaillink = document.getElementById("email");
emaillink.onmouseover = hover;
emaillink.onmouseout = cleardesc;
complainlink = document.getElementById("complain");
complainlink.onmouseover = hover;
complainlink.onmouseout = cleardesc; html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Descriptive Links</TITLE>
</HEAD> <BODY>
<h1>Descriptive Links</h1>
<p>Move the mouse pointer over one of these links to view a description:</p>
<ul>
<li><a href="order.html" id="order">Order Form</a>
<li><a href="email.html" id="email">Emainl</a>
<li><a href="complain.html" id="complain">Complain</a>
</ul>
<h2 id="description">Welcome to Js word!</h2>
<script language="javascript" type="text/javascript" src="linkdesc.js">
</script>
</BODY>
</HTML>
请各位指教啊!!谢谢!!!
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>Descriptive Links</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD>
<script type="text/javascript">
function cleardesc() {
d = document.getElementById("description");
d.innerHTML = "Welcome to Js word!";
}function hover(e) {
var desc;
if (!e) var e = window.event;
//which link was the mouse over?
if (e.target) {
whichlink = e.target.id;
}
else {
whichlink = e.srcElement.id;
} //choose the appropriate description
switch (whichlink) {
case "order":
desc = "Order a product";
break;
case "email":
desc = "Send us a message";
break;
case "complain":
desc = "Insult us, our products,or our families";
break;
default:
desc = "Error!";
break;
} //display the description in the H2
d = document.getElementById("description");
d.innerHTML = desc;
}//set up the event handlers
window.onload = function(){
orderlink = document.getElementById("order");
orderlink.onmouseover = hover;
orderlink.onmouseout = cleardesc;
emaillink = document.getElementById("email");
emaillink.onmouseover = hover;
emaillink.onmouseout = cleardesc;
complainlink = document.getElementById("complain");
complainlink.onmouseover = hover;
complainlink.onmouseout = cleardesc;
};
</script>
<BODY>
<h1>Descriptive Links </h1>
<p>Move the mouse pointer over one of these links to view a description: </p>
<ul>
<li> <a href="order.html" id="order">Order Form </a>
<li> <a href="email.html" id="email">Emainl </a>
<li> <a href="complain.html" id="complain">Complain </a>
</ul>
<h2 id="description">Welcome to Js word! </h2>
</BODY>
</HTML>
嗯嗯!可以了,谢谢啊!但是我不太明白,这是为什么啊!???能解释一下吗?我是新手,不太明白下面这个:
window.onload = function()
意思是当文档加载完毕后执行函数,没有这个语句,当js遇到innerHTML就开始执行,而此时的idnum指向的元素还没有被完全加载,所以会报错。
但是我的<script>标签本来就放到html的最后了啊我以为是文档加载完了才加载js,还请高手详细的讲解一下!谢谢