jQuery: If this HREF contains

前端 未结 6 1832
南笙
南笙 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:16

    You could just outright select the elements of interest.

    $('a[href*="?"]').each(function() {
        alert('Contains question mark');
    });
    

    http://jsfiddle.net/mattball/TzUN3/

    Note that you were using the attribute-ends-with selector, the above code uses the attribute-contains selector, which is what it sounds like you're actually aiming for.

提交回复
热议问题