简体   繁体   English

未捕获的TypeError:对象函数()没有方法“替换”

[英]Uncaught TypeError: Object function () has no method 'replace'

I'm using a jQuery chained select dropdown box from this site / script . 我正在使用一个来自此站点 / 脚本的jQuery链式选择下拉框。 I put it in the sidebar and it works fine on the homepage but it isn't working on post pages and the debugger points out this error. 我将其放在侧边栏中,并且在首页上运行良好,但在帖子页上却无法正常运行,调试器指出了此错误。

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'

It says there's an error in escapeQuotes : function(str) {return str.replace(/([""\\\\])/g, "\\\\$1"); 它说escapeQuotes : function(str) {return str.replace(/([""\\\\])/g, "\\\\$1");有一个错误escapeQuotes : function(str) {return str.replace(/([""\\\\])/g, "\\\\$1");

The beginning of the script: 脚本的开头:

(function($) {
  $.dynamicDropdown = {
    /**
     * Escape quotation marks and slashes
     * @param {String} String to format
     * @return {String}
     */
    escapeQuotes : function(str) {
      return str.replace(/([""\\])/g, "\\$1");
    },

Here's how I call the function. 这就是我调用该函数的方式。 I'm using a json file to pull the options text and value into the selected boxes : 我正在使用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":"|"});

    });
});

Edited: 编辑:

It seems that there's a conflict with a random image rotator I just put on the site. 似乎与我刚刚在网站上投放的随机图片旋转器发生冲突。 I temporarily removed the rotator and the chained select box is working fine. 我暂时移除了旋转器,链接的选择框运行正常。 Here's an example to show the error. 这是显示错误的示例 And this is without the random rotator. 是没有随机旋转器的。

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];
                }

            }

in this script which you use, the problem is most likely in these lines of code: 在您使用的此脚本中 ,最有可能是这些代码行出现了问题:

for (var i in parts) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

The point is, do not iterate over an array using for in loop, since there is probably a function added to the Array.prototype which shows up in for in loop over the array, simply change it to: 关键是,不要使用for in循环遍历数组,因为可能在Array.prototype中添加了一个函数,该函数在数组的for in循环中显示,只需将其更改为:

for (var i=0;i<parts.length;i++) {
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

then this won't cacth that function any more. 那么这将不再起作用。


as you added to your post, the reason is exactly what I have pointed out. 当您添加到帖子中时,原因正是我所指出的。 but if you still insist on using for in loop, you should check the type of parts[i] like this: 但是,如果您仍然坚持使用for in loop,则应像下面这样检查parts[i]的类型parts[i]

for (var i in parts) {
      if(typeof parts[i] != "string") continue;
      name += "[\"" + $.dynamicDropdown.escapeQuotes(parts[i]) + "\"]";
  ...
}

you have the same problem with another for in loop: 您在另一个for in循环中也遇到相同的问题:

for (var i in options) {
    option = $(document.createElement("option"))
      .val($.isArray(options[i]) ? i : options[i])
      .html(i)
      .appendTo(select);
}

change it to for (var i=0;i<options.length;i++) or add this: 将其更改为for (var i=0;i<options.length;i++)或添加以下内容:

if(typeof options[i] != "string") continue;

to the first line of your for loop. 到您的for循环的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM