Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用元素选择器的条件格式_Jquery - Fatal编程技术网

Jquery 使用元素选择器的条件格式

Jquery 使用元素选择器的条件格式,jquery,Jquery,我试图突出显示所有未设置href的链接,或者如果该链接为空。我写了以下内容: 我注意到if语句似乎只适用于最先出现的链接,下面所有链接的样式都相同。第二组“蓝色”与第一组相同,但顺序已切换 我的方法显然不对。是否可以在每个元素上有条件地应用yellow类(基于href是否为空? $("a").each(function(){ if(!($(this).attr("href")) || $(this).attr("href") == "") { $(th

我试图突出显示所有未设置
href
的链接,或者如果该链接为空。我写了以下内容:

我注意到if语句似乎只适用于最先出现的链接,下面所有链接的样式都相同。第二组“蓝色”与第一组相同,但顺序已切换

我的方法显然不对。是否可以在每个元素上有条件地应用yellow类(基于href是否为空?

    $("a").each(function(){

       if(!($(this).attr("href")) || $(this).attr("href") == "") {
          $(this).addClass("yellow");
       }       
    });
更新的JSFIDLE:

根据评论中的建议编辑上述代码

 $("a").each(function(){
    var self = $(this).attr("href");
    if(!(self) || self == "") {
        $(this).addClass("yellow");
    }        
});
这有帮助吗

    $("a").each(function(){

       if(!($(this).attr("href")) || $(this).attr("href") == "") {
          $(this).addClass("yellow");
       }       
    });
更新的JSFIDLE:

根据评论中的建议编辑上述代码

 $("a").each(function(){
    var self = $(this).attr("href");
    if(!(self) || self == "") {
        $(this).addClass("yellow");
    }        
});

你好,演示

代码

$("a").each(function(){
    //do something with the element here.
    if (!($(this).attr("href")) || $(this).attr("href") == ""){
        // now blue and green diff is not needed because you append yellow if they are empty.
         $(this).attr('class', 'yellow');
    }

});
​

你好,演示

代码

$("a").each(function(){
    //do something with the element here.
    if (!($(this).attr("href")) || $(this).attr("href") == ""){
        // now blue and green diff is not needed because you append yellow if they are empty.
         $(this).attr('class', 'yellow');
    }

});
​
等等。。什么

jQuery选择器不能完成这项工作吗

$('.blue[href=”“]).addClass('yellow')

等等。。什么

jQuery选择器不能完成这项工作吗


$('.blue[href=”“]).addClass('yellow')

我唯一要说的是,缓存
$(this.attr('href')
可能有意义。否则,+1比我要去的地方效率高。=)每天学一件新东西:)谢谢!我唯一要说的是,缓存
$(this.attr('href')
可能有意义。否则,+1比我要去的地方效率高。=)每天学一件新东西:)谢谢!:(未定义变为undefined@MiaDiLorenzoaha将更新它,这是我删除的唯一一个树脂t,因为我认为href将始终与最终对象在一起。:)干杯(未定义变为undefined@MiaDiLorenzoaha将更新它,这是我删除的唯一一个树脂t,因为我认为href将始终与最终对象在一起。:)干杯!如何处理href未定义(第3个)的情况?$('.blue:not([href]))。addClass('yellow')Fizzle在大多数情况下可能比循环更快:我是密密麻麻的。如何将[href=”“]与:not([href])?逗号组合$('.blue[href=”“],.blue:not([href])).addClass('yellow');如何处理href未定义(第3个)的情况?$('.blue:not([href]))。addClass('yellow')Fizzle在大多数情况下可能比循环更快:我是密密麻麻的。如何将[href=”“]与:not([href])?逗号组合$('.blue[href=”“],.blue:not([href])).addClass('yellow');