I want to be able to change all the anchor's properties on a page. But I don't know how to loop through all of them.
используйте каждый:
$("a").each(function(){
//do something with the element here.
});
jQuery изначально предоставляет эту возможность.
$('a').do_something();
Будет do_something()
выполняться для каждого a
на странице. Итак:
$('a').addClass('fresh'); // adds "fresh" class to every link.
Если то, что вы хотите сделать, требует просмотра свойств каждого a
по отдельности, используйте .each()
:
$('a').each( function(){
var hasfoo = $(this).hasClass('foo'); // does it have foo class?
var newclass = hasfoo ? 'bar' : 'baz';
$(this).addClass(newclass); // conditionally add another class
});
$('a').each(function(i){
$(this).attr('href','xyz');
});