這已被問過很多次了,但我是一個非常熱衷於學習的新手,所以我希望有人可以花時間解釋這一點對我來說。我還無法使用當前可用的答案找出解決此特定錯誤的方案...Uncaught TypeError:無法讀取undefined的屬性'split' - 希望上一課
應顯示在圖像庫標題下的圖像庫未顯示,錯誤爲:
Uncaught TypeError: Cannot read property 'split' of undefined
at HTMLAnchorElement.<anonymous> (scripts.js:1604)
at Function.each (jquery.js:2)
at a.fn.init.each (jquery.js:2)
at markScrollToTargets (scripts.js:1602)
at HTMLDocument.<anonymous> (scripts.js:1552)
at i (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at Function.ready (jquery.js:2)
at HTMLDocument.K (jquery.js:2)
有人能告訴我如何解決這個問題嗎?而且,爲什麼發生?
我相信這是相關代碼:
function markScrollToTargets() {
var scrollToSelector = '#';
var $aTags = $('nav a'); // get all level anchor tags
var url = window.location.href;
var urlSplitArray = url.split(scrollToSelector);
url = urlSplitArray[0];
var $target;
$aTags.each(function (index) {
var $this = $(this);
var splitArray = $this.attr('href').split(scrollToSelector);
var thisURL = splitArray[0];
var thisTargetClass = splitArray[1];
if (typeof thisTargetClass != "undefined") { // check if link has a scrollto selector
if (thisTargetClass != "") { // check if link has scrollto selector but no actual target class (like just #-link)
if (thisURL != "") { // if link has url before scrollto selector
if (thisURL == url) { // only consider if the link url is the same as current url
$("." + thisTargetClass).addClass('scroll-to-target').addClass('scroll-to-target-class-' + thisTargetClass);
}
} else {
$("." + thisTargetClass).addClass('scroll-to-target').addClass('scroll-to-target-class-' + thisTargetClass);
}
}
}
});
}
將能夠顯示您的代碼?沒有它,幾乎不可能回答這個問題。 –
確保您調用'.split'的值不是'undefined'。你要麼沒有傳遞正確的值,要麼只有有時候這個值是'未定義的',你需要警惕這一點,並決定你想要做什麼,以防它是'未定義'。 –
您認爲的代碼包含您的錯誤。我會想象它是你想要使用拆分方法的地方。 –