jQuery if属性包含某个值

Kev*_*vin 2 jquery

这里总有一个空白.希望你能提供帮助.

当属性'href不以#overlay'开头时,我如何将此参数更改为...

if(this.getTrigger().attr("href")){
// stuff in here
}
Run Code Online (Sandbox Code Playgroud)

谢谢你们精彩的人.凯文

And*_*y E 8

您可以使用slice,substringsubstr:

if (this.getTrigger().attr("href").slice(0, 8) != "#overlay") {
}
Run Code Online (Sandbox Code Playgroud)

或者indexOf:

if (this.getTrigger().attr("href").indexOf("#overlay") != 0) {
}
Run Code Online (Sandbox Code Playgroud)

或正则表达式test方法:

if (!/^#overlay/.test(this.getTrigger().attr("href"))) {
}
Run Code Online (Sandbox Code Playgroud)

  • `substring`是最快的(当考虑失败和成功搜索时):http://jsperf.com/js-startswith/6 (2认同)