简体   繁体   中英

hide parent div if link inside has empty href

I need to hide the buttonholder Div which is styled to look like a button. But the button styles images need to hide if the link itself is empty.

<div class="RegisterBtnHolder"> 
    <span class="RegisterOrangeButton">
             <span>
              <a href="http://www.google.com">Register Online</a>
       </span>                              
    </span>
</div>

I need to hide RegisterBtnHolder if the anchor tag has empty href or empty text..How do i do this in jquery.

give this a shot:

$(function(){
       $("a[href=''],a:empty","div.RegisterBtnHolder").closest("div.RegisterBtnHolder").hide();
    });

Using jQuery:

var button = $('.RegisterBtnHolder').find('a'); // caches the <a> element from the dom.
if(button.attr('href') == '') {
    button.hide();
}

The above answer prolly works aswell, just remember try to avoid jumping into the DOM as much as possible, it will slow down your load time.

这对您有用吗:

if ($('div.RegisterBtnHolder a').text() == '' || $('div.RegisterBtnHolder a').attr('href') == '') $('div.RegisterBtnHolder a').hide()​

sample code below

if($("a").attr("href") === "" || $("a").text()===""){
  $(this).closest("div").hide();   
}

Fiddle

$('.RegisterBtnHolder a').each(function() {
    if($(this).attr('href') === '' || $(this).text() === '') {
        $(this).parents('.RegisterBtnHolder').hide();
    }
});

Useing filter() helps

http://api.jquery.com/filter/

$('.RegisterBtnHolder a').filter(function(){
     /* add any additional tests you might need such as looking for "#" as an href*/    
    return $(this).attr('href')=='' || $.trim($(this).text())=='';                        
}).closest('.RegisterBtnHolder').hide();

JavaScript Only

var dilly = document.querySelectorAll('.RegisterBtnHolder a'), i;

for (i = 0; i < dilly.length; ++i) {
     var $true = (dilly[i].getAttribute('href') == '')
     if ($true == true) {
          dilly[i].parentElement.style.display = 'none'
     } else {
          dilly[i].parentElement.style.border = "1px dotted silver"
     }
}

jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM