收到错误"has no method 'contains'"



我正在尝试使用以下代码片段:

console.log(config.url);
if (config.method === 'GET' && config.url.contains("/Retrieve")) {

它输出:

/Content/app/home/partials/menu.html app.js:255
TypeError: Object /Content/app/home/partials/menu.html has no method 'contains'
    at request (http://127.0.0.1:81/Content/app/app.js:256:59)

但是,这给了我".contains"一词的错误。有人知道我为什么收到这条消息吗?

has no method 'contains'

有谁知道为什么我会收到这条消息?

因为在 JavaScript 中,字符串没有contains方法。您可以使用indexOf

if (config.method === 'GET' && config.url.indexOf("/Retrieve") !== -1) {

请注意,这区分大小写。要做到这一点而不必担心大写,您可以使用toLowerCase()

if (config.method === 'GET' && config.url.toLowerCase().indexOf("/retrieve") !== -1) {

。或带有 i 标志的正则表达式(区分大小写):

if (config.method === 'GET' && config.url.match(//Retrieve/i)) {

问题是包含是一个jQuery函数,所以你只能把它应用于jQuery对象。

如果你想使用纯javascript,你必须使用indexOf 函数

contains() 不是原生的 JavaScript 方法;它被 JS 库(如 jQuery)使用。

如果你只是使用纯JavaScript,那么你可以使用像indexOf()这样的方法。

最新更新