JQuery:如果href为空则隐藏锚点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7586136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:01:36  来源:igfitidea点击:

JQuery: Hide anchor if href is empty

jqueryhideshow-hide

提问by Guy

Been on this one for a while now. Basically, I need to check where a hrefon an anchor tag with the class .pdf-downloadis empty, and if it is, hide it.

已经在这个上一段时间了。基本上,我需要检查href带有类的锚标记上的a在哪里.pdf-download为空,如果是,则将其隐藏。

I have attempted a few options but with no luck. This is what i have so far:

我尝试了一些选择,但没有运气。这是我到目前为止:

$("a.pdf-download").each(function (i) {
  if ($('[href]:empty',this).length == 1) { 
    $(this).hide();
  } else {
    $(this).show();
  }
});

回答by amosrivera

if ($(this).attr('href') != '') { 
    $(this).hide();
} else {
    $(this).show();
}

Note that you can also do this in css with attribute selectors:

请注意,您也可以使用属性选择器在 css 中执行此操作:

a.pdf-download[href='']{
    display:none;
}

This is not supported in ie6 though.

但是 ie6 不支持此功能。

回答by Elliot Nelson

You can also do this with a jQuery selector, as follows:

您也可以使用 jQuery 选择器执行此操作,如下所示:

// Hide any links with blank href or no href attribute
$('a.pdf-download[href=], a.pdf-download:not([href])').hide();

回答by Rob W

Use this solution. it will also produce the desired results when the hrefattribute is not defined. If you use a CSS selector (JQuery), non-existent hrefattributes will not be detected.

使用此解决方案。当href属性未定义时,它也会产生预期的结果。如果您使用 CSS 选择器 (JQuery),href则不会检测到不存在的属性。

$("a.pdf-download").each(function (i) {
    if (!this.href) { 
        $(this).hide();
    } else {
        $(this).show();
    }
})

It's not necessary to use a JQuery method to get the hrefattribute, becausethis.hrefis just as readable, faster and also universally supported.

没有必要使用 JQuery 方法来获取href属性,因为它this.href具有同样的可读性、速度和普遍支持。

回答by Matt

Would something like this work?

这样的东西会起作用吗?

$("a.pdf-download").each(function (i) {
  if ($(this).attr('href').length == 0) { 
    $(this).hide();
  } else {
    $(this).show();
  }
});

回答by David Houde

$("a.pdf-download").each(function() {
    var href = $(this).attr("href");
    if(href == '') {
        $(this).remove();
    }
});

or

或者

$("a.pdf-download[href='']").remove() 

回答by daryl

$(function() {

    $('a').each(function() {
        (!$(this).attr('href')) ? $(this).hide() : $(this).show();
    });

});

Almighty Demo: http://jsfiddle.net/each/j9DGw/

全能演示:http: //jsfiddle.net/each/j9DGw/

回答by r0skar

I am a jquery beginner myself, but thats how I would do it:

我自己是一个 jquery 初学者,但这就是我要做的:

$("a.pdf-download").each(function (i) {

    var aHref = $(this).attr('href');

    if (aHref == '' || !aHref) {

        $(this).hide();

    };

});

Demo: http://jsfiddle.net/BZq9c/1/

演示:http: //jsfiddle.net/BZq9c/1/