我需要使用jquery遍历我的网站,并更改所有具有href=#的<a>元素的类选择器。我有这么多,但不确定如何在jquery中编写"for/each“部分。外面有什么帮助吗?
if($("a.cs-wowslider-images-new").attr('href') == "#"){
$("a.cs-wowslider-images-new").removeClass("wow-fancy");
alert('removed');
}发布于 2011-03-20 06:14:51
您遗漏的...all是.each()方法:
$("a").each(function(idx) {
if ($(this).attr('href') == "#") {
$(this).removeClass('whateverClass');
}
});发布于 2011-03-20 06:22:03
如果所需的操作也可以作为jQuery函数使用,则不需要each块:
$('a[href="#"]').removeClass('wow-fancy');jQuery函数需要传递匹配元素的数组,并且在大多数情况下会自动将该函数应用于该数组中的每个元素。
发布于 2011-03-20 06:18:43
我想你应该看看一些attribute selectors。例如,下面的选择器使用Attribute Starts With Selector返回指向锚点(以#开头指向命名锚点)的所有链接
$('input[href^="#"]').each(function() {
// do something with $(this)
});https://stackoverflow.com/questions/5365267
复制相似问题