更新:重新您在更新後的代碼問題:
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function") {
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
}
// ====> This is where the bug is, you're assuming you have
// a string at this point, but you don't necessarily,
// because if there is no match at all, `attr` will
// return `undefined` or `null` (I forget which).
//do the replace
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
//change oncontextmenu with the new value
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
})
}
}
修復:
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function") {
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
}
// ====> Add this `if`:
if (oldName) {
//do the replace
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
//change oncontextmenu with the new value
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
});
}
}
}
原來的答覆:
在我看來,你錯過了一些括號(至少):
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function") { // <=== Added { here
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
})
} // <=== Added } here
}
}
這裏是上面修正後的縮進:
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function") { // <=== Added { here
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
})
} // <=== Added } here
}
}
這是您的原始與修正的縮進:
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function")
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
// Note how these statements are NOT protected by the `if`
// above, and so if `oldName` is `undefined` (as opposed to
// being a String or Function.
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
})
}
}
或者,它可能是你缺少一個else
條款並可能進一步防禦if
:
function replaceRightClickIcefacesMethod() {
if (jQuery.browser.msie) {
var elem = jQuery(".singlePaneOfGlassBlock"), oldName = elem
.attr("oncontextmenu"), fn = String;
if (typeof oldName == "function") {
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
}
else {
oldName = oldName.replace('Ice.Menu.contextMenuPopup',
'contextMenuPopupUpdated');
}
if (oldName) {
jQuery.each(elem, function() {
this.setAttribute("oncontextmenu", fn(oldName));
});
}
}
}
但它真的很難說,我不能完全遵循的邏輯。它的功能路徑部分似乎沒有改變任何東西,但字符串路徑部分...
謝謝,我已經添加了大括號,但在正確的位置爲我的情況。但是,npe錯誤並不是因爲這個.. – 2011-05-05 13:30:31
@Cristian,@Alnitak:我已經更新了一下。 @克里斯蒂安:如果你退後一步並描述你實際想要做什麼(可能作爲一個單獨的問題),我認爲很可能有更好的方法來處理它。 – 2011-05-05 13:32:08
@Cristian:重新更新你的代碼:你仍然假設你找回了一個'Function'或'String';看到我更新的答案。 – 2011-05-05 13:36:35