jQuery: If this HREF contains

前端 未结 6 1861
南笙
南笙 2021-01-30 20:51

Why can\'t I get this to work??

$(\"a\").each(function() {
    if ($(this[href$=\"?\"]).length()) {
        alert(\"Contains questionmark\");
    }
});
         


        
6条回答
  •  情书的邮戳
    2021-01-30 21:28

    It doesn't work because it's syntactically nonsensical. You simply can't do that in JavaScript like that.

    You can, however, use jQuery:

      if ($(this).is('[href$=?]'))
    

    You can also just look at the "href" value:

      if (/\?$/.test(this.href))
    

提交回复
热议问题