javascript開發中因空格引發的錯誤

來源:互聯網
上載者:User

廢話不多說,先上代碼給大家看看 複製代碼 代碼如下:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>依依園地</title>
</head>
<body>
<div id="win">
<ul id="picChange">
<li>
<img src="images/01.jpg" title="圖片1"></li>
<li>
<img src="images/02.jpg" title="圖片2"></li>
<li>
<img src="images/03.jpg" title="圖片3"></li>
</ul>
</div>
<script type="text/javascript">
var pic = document.getElementById('picChange');
var picList = pic.getElementsByTagName("li");
alert(picList[0].firstChild);
</script>
</body>
</html>

以上代碼1請分別在Firefox和IE中運行,Firefox出現的是 [object Text] ,而IE中出現的是[object],這裡IE和Firefox都把picList[0].firstChild這個對象當作object對象,Firefox提示是Text類型對象,IE沒有提示更細了。
我們再運行下面這部分的代碼看看,看清楚了,代碼本身相同,但是li下面沒做換行縮排。 複製代碼 代碼如下:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>依依園地</title>
</head>
<body>
<div id="win">
<ul id="picChange">
<li><img src="images/bigpic/01.jpg" title="護腰/HY005"></li>
<li><img src="images/bigpic/02.jpg" title="枕頭吊臂/DB-004"></li>
<li><img src="images/bigpic/03.jpg" title="大護腳/HY-001"></li>
</ul>
</div>
<script type="text/javascript">
var pic = document.getElementById('picChange');
var picList = pic.getElementsByTagName("li");
alert(picList[0].firstChild);
</script>
</body>
</html>

以上代碼2請分別在Firefox和IE中運行,Firefox出現的是 [object HTMLImageElement] ,而IE中出現的是[object],這裡IE和Firefox都把picList[0].firstChild這個對象當作object對象,Firefox提示是HTMLImageElement類型對象,IE沒有提示更細了。
上下兩種寫法只是縮排和換行的差別,在IE中被認做相同的object對象,可是不同的縮排在Firefox中卻代表了不同的對象,Firefox對HTML的解析實在令人費解。
再此做記錄,希望各位朋友看了有用,別因為跟我一樣的問題半天沒找到解決問題的方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.