我正在使用jQuery鏈接選擇下拉框this site/script。我把它放在側邊欄中,它在主頁上工作正常,但它不適用於後期頁面,調試器指出這個錯誤。Uncaught TypeError:對象函數()沒有方法'替換'
Uncaught TypeError: Object function()
{for(var a=[];this.length;)a.push(this.splice(Math.random()*this.length,1));
for(;a.length;)this.push(a.pop());return this} has no method 'replace'
它說,有一個在escapeQuotes : function(str) {return str.replace(/([""\\])/g, "\\$1");
腳本的開頭的錯誤:
(function($) {
$.dynamicDropdown = {
/**
* Escape quotation marks and slashes
* @param {String} String to format
* @return {String}
*/
escapeQuotes : function(str) {
return str.replace(/([""\\])/g, "\\$1");
},
這是我如何調用該函數。我使用JSON文件的選項文本和值拉入選擇框:
$(document).ready(function(){
$.getJSON('suggest.json', function(data){
var $select = $('#mySelectID');
$.each(data, function (index, o) {
var $option = $("<option/>").attr("value", o.Box1ID + ":" + o.Box3).text(o.Box1 + "|" + o.Box2 + "|" + o.Box3);
$select.append($option);
});
$("#mySelectID").dynamicDropdown({"delimiter":"|"});
});
});
編輯:
似乎有一個隨機圖像旋轉我只是把網站上的衝突。我暫時刪除了旋轉器和鏈接選擇框工作正常。這裏有一個example來顯示錯誤。並且this沒有隨機旋轉器。
Array.prototype.shuffle = function() {
var s = [];
while (this.length) s.push(this.splice(Math.random() * this.length, 1));
while (s.length) this.push(s.pop());
return this;
}
var picData = [
['img1','url_1'],
['img2','url_2'],
['img3','url_3'],
picO = new Array();
randIndex = new Array(); //array of random indexes
for(i=0; i < picData.length; i++){
picO[i] = new Image();
picO[i].src = picData[i][0];
picO[i].alt = picData[i][1];
randIndex.push(i);
}
randIndex.shuffle();
window.onload=function(){
var mainImgs = document.getElementById('carouselh').getElementsByTagName('img');
for(i=0; i < mainImgs.length; i++){
mainImgs[i].src = picO[randIndex[i]].src; //assign a random image
mainImgs[i].parentNode.href = picData[randIndex[i]][1];
mainImgs[i].alt = picData[randIndex[i]][1];
}
}
'str'是一個數組,一個對象,null或false。 – Ohgodwhy
將錯誤的值傳遞給'escapeQuotes'。 'str'是一個函數而不是一個字符串。確保你正確地調用了函數,即傳遞一個字符串(因爲你沒有顯示函數是如何調用的,這就是所有可以說的)。 –
@ FelixKling謝謝。我只是增加了額外的細節。 – RedGiant