jQuery 如果文档标题索引匹配字符串

jQuery if document title indexof matches string

所以基本上如果文档标题完全匹配,就做点什么。

我正在尝试使用 indexOf,但无论页面标题是什么,它都会继续运行。

例如,在这个页面上,我希望它仅在文档标题为 if 语句时继续 - jQuery 如果文档标题索引匹配字符串 - Stack Overflow(现在是):

if (document.title.indexOf("if statement - jQuery if document title indexof matches string - Stack Overflow") === -1){
    alert("Hello! I am an alert box!!");
}

嘿,只需将您的代码更改为:

if (document.title.indexOf("if statement - jQuery if document title indexof matches string - Stack Overflow") != -1){
alert("Hello! I am an alert box!!");}

indexOf 方法returns -1 如果要搜索的值从未出现过。因此,如果不是 -1,则表示包含该值。

注意:如果您想要完全匹配,请使用“==”运算符而不是 indexOf

干杯!