读书人

初学者学做列表但是提示异常

发布时间: 2012-03-29 12:53:12 作者: rapoo

菜鸟学做列表,但是提示错误?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> Untitled Page </title>
<script language= "javascript " type= "text/javascript ">
// <!CDATA[

function b1_ondblclick() {
if(b1.innerText== "+ ")
{
b1.innerText= "- ";
list1.style.display= "block ";
}
else
{
b1.innerText== "+ "
list1.style.display= "none ";
}


}

// ]]>
</script>
</head>
<body>
<span id=b1 onclick= "return b1_onclick() " style= "border-right: thin ridge; border-top: thin ridge; border-left: thin ridge; border-bottom: thin ridge; cursor: hand; "> - </span> list1 <br />
<ul id=list1>
<li>
item1 <br /> </li>
<li> item2 <br /> </li>
<li> item3 </li>
</ul>
list2

</body>
</html>


[解决办法]


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> Untitled Page </title>
<script language= "javascript " type= "text/javascript ">
// <!CDATA[

function b1_ondblclick() {
if(b1.innerText== "+ "){
b1.innerText= "- ";
list1.style.display= "block ";
}
else
{
b1.innerText= "+ "//这个错误太低级了b1.innerText== "+ "
list1.style.display= "none ";
}
}

// ]]>
</script>
</head>
<body>
<span id=b1 onclick= "return b1_ondblclick() " style= "border-right: thin ridge; border-top: thin ridge; border-left: thin ridge; border-bottom: thin ridge; cursor: hand; "> - </span> list1 <br />
<ul id=list1>
<li>
item1 <br /> </li>
<li> item2 <br /> </li>
<li> item3 </li>
</ul>
list2

</body>
</html>

[解决办法]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> Untitled Page </title>
<script type= "text/javascript ">
// <!CDATA[

function b1_ondblclick()
{
var b1 = document.getElementById( 'b1 ');
var list1 = document.getElementById( 'list1 ');


alert(b1.innerHTML);
if(b1.innerHTML== "+ ")
{
b1.innerHTML= "- ";
list1.style.display= "block ";
}
else
{
b1.innerHTML = "+ "
list1.style.display= "none ";
}
}

// ]]>
</script>
</head>
<body>
<span id=b1 onclick= "b1_ondblclick() " style= "border-right: thin ridge; border-top: thin ridge; border-left: thin ridge; border-bottom: thin ridge; cursor: hand; "> - </span> list1 <br />
<ul id=list1>
<li>
item1 <br /> </li>
<li> item2 <br /> </li>
<li> item3 </li>
</ul>
list2
</body>
</html>
[解决办法]
1. b1.innerText== "+ " // 这个有错误
2.页面对象用document.getElementById去取
如:
var b1 = document.getElementById( 'b1 ');
var list1 = document.getElementById( 'list1 ');

3.最好不要用innerText 用innerHTML 有的浏览器不支持这个属性

读书人网 >JavaScript

热点推荐