程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> javascrpit-請問代碼在兩種環境下執行不一樣啊?

javascrpit-請問代碼在兩種環境下執行不一樣啊?

編輯:編程綜合問答
請問代碼在兩種環境下執行不一樣啊?

原意是在DW中圖片可以在頁面中切換,而在chrome中就變成直接跳轉到圖片了,頁面就不存在了。
圖片說明
圖片說明

關鍵代碼如下:

function showPic(whichpic) {

var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
var text = whichpic.getAttribute("title");
var description = document.getElementById("description");
placeholder.setAttribute("src",source);
description.firstChild.nodeValue = text;

}

而多添加了些判斷就能解決這問題,這是我不理解的地方。
代碼如下
function showPic(whichpic) {

var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src",source);

if (whichpic.getAttribute("title")) {
var text = whichpic.getAttribute("title");
} else {
var text = "";
}
var description = document.getElementById("description");
if (description.firstChild.nodeType == 3) {
description.firstChild.nodeValue = text;
}
return false;
}

還望解答,至誠感謝。

最佳回答:


典型的js hack
IE的getAttribute有bug

http://blog.csdn.net/zxsrendong/article/details/6650516

whichpic.getAttribute("title")
這裡判斷下避免返回null

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved