为什么我会得到错误“;未捕获的类型错误:st.replace不是函数;

Why am I getting the error "Uncaught TypeError: st.replace is not a function"

本文关键字:错误 st 类型 replace 函数 为什么      更新时间:2023-09-26

我试图构建一个投资组合。我不是javascript专家,也不知道该删除什么,或者是哪里出了问题。页面运行良好,但一旦我尝试转到主页,退出速度非常慢,控制台会抛出一个错误。

"未捕获类型错误:st.replace不是函数"

下面的这个片段是有错误的,当我单击主页按钮离开"服务"页面时,它就会给我错误。

感谢您对摘录中错误内容的任何说明。

            htmlDoc: function(st) {
                var n, i = e(),
                    r = /<('/?)(html|head|body|title|base|meta)('s+[^>]*)?>/gi,
                    o = "ss" + Math.round(1e5 * Math.random()),
                    a = st.replace(r, function(t, n, r, a) {
                        var s = {};
                        return n || (e.merge(i, e("<" + r + "/>")), a && e.each(e("<div" + a + "/>")[0].attributes, function(e, t) {
                            s[t.name] = t.value
                        }), i.eq(-1).attr(s)), "<" + n + "div" + (n ? "" : ' id="' + o + (i.length - 1) + '"') + ">"
                    });
                return i.length ? (n || (n = e("<div/>")), n.html(a), e.each(i, function(e) {
                    var t = n.find("#" + o + e).before(i[e]);
                    i.eq(e).html(t.contents()), t.remove()
                }), n.children().unwrap()) : e(t)
            },

编辑FelixKling

谢谢,@FelixKling似乎有几个值:

"st=/-([''da-z])/gi,"

"st=":("+rt+"

"St=["顶部","右侧","底部","左侧"],"

但最后一个有一个大写字母S。请把这个答案复制粘贴,以便读得更清楚。如果我需要解释更多,请告诉我。

您的问题似乎是st不是字符串。在函数顶部添加一条验证行,以确保参数的类型为所需类型。

if (typeof st !== 'string') {
    // HANDLE IT HERE (throw an error, return false/null, whatever you need
}