.each loop in jquery that finds all anchors pointing to specifc location and changes the class
I need to loop through my website using jquery and change the class selector of all <a>
elements which have href=#
. I have this much but am not sure how to write the "for/each" portion in jquery.. any help out there?
if($("a.cs-wowslider-images-new").attr('href') == "#"){
$("a.cs-wowslider-images-new").removeClass("wow-fa开发者_StackOverflow中文版ncy");
alert('removed');
}
...all you were missing was the .each() method:
$("a").each(function(idx) {
if ($(this).attr('href') == "#") {
$(this).removeClass('whateverClass');
}
});
There's no need for an each
block if the desired operation is also available as a jQuery function:
$('a[href="#"]').removeClass('wow-fancy');
jQuery functions expect to be passed an array of matching elements, and in most cases will automatically apply the function to each element on that array.
I think you want to look at some of the attribute selectors. E.g. the following selector returns all links that point to an anchor (href starting with # pointing to a named anchor) using the Attribute Starts With Selector:
$('input[href^="#"]').each(function() {
// do something with $(this)
});
精彩评论