我有一组按钮都有相同的类名,如果没有URL,有时需要灰显一个按钮。我试图通过向该特定按钮添加一个css类.not-available来做到这一点。但这也会将类名添加到所有其他按钮。代码如下:
$('a').each(function(index) {
if( $(this).attr('href') == '' ) {
$('.button').addClass('not-available')
}发布于 2017-08-01 21:02:48
您的if语句正在引用$('.button'),这将影响.button的所有实例,而不仅仅是那些href为空的实例。将您的选择器更改为$(this),它将只针对与您的条件匹配的那些。
$('a').each(function(index) {
if( $(this).attr('href') == '' )
{
$(this).addClass('not-available')
}发布于 2017-08-01 21:21:38
这将会起作用并且我测试了..。使用"find()“方法实现这一点...
$('a').each(function(index) {
if($(this).attr('href') == '') {
$(this).find(".button").addClass('not-available');
}
}https://stackoverflow.com/questions/45438568
复制相似问题