2013-08-27 27 views
6

Jak mogę ukryć element div i wyświetlać go tylko wtedy, gdy na stronie istnieje inny element div? Zgaduję jQuery lub js byłoby do zrobienia ....Wyświetl element tylko wtedy, gdy istnieje inny element.

<style type="text/css"> 
.always-here { 
    display:none; 
} 
</style> 

<div class="im-here">This div exists on this particular page!</div> 
<div class="always-here">This div is always here but has the style 
display: none unless a div with the class "im-here" exists.</div> 

Odpowiedz

7

dla bieżącej html obecnej można zrobić

.always-here { 
    display:none; 
} 
.im-here ~ .always-here{ 
    display:block; 
} 

ten zadziała tylko wtedy, gdy .always-here i .im-here są rodzeństwem i .im-here wcześniej.

http://jsfiddle.net/BKYSV/ - .im-here obecny
http://jsfiddle.net/BKYSV/1/ - .im-here nieobecny

+0

Wow, nie miał pojęcia, że ​​można to rozwiązać za pomocą CSS. Czy ta metoda jest zgodna z przeglądarką? – Heather

+0

@Heater patrz http://reference.sitepoint.com/css/generalsiblingselector, https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors, https://www.google.tt/ search? q = general + sibling + selector i oq = general + sibling + selector & aqs = chrome..69i57j0l3.6681j0 & sourceid = chrome & ie = UTF-8 # fp = 828651ec3b51e1b8 & q = css + ogólne + sibling + selektor + przeglądarka + wsparcie – Musa

0

Spróbuj tego:

if($(".im-here").length) 
$(".always-here").show(); 
1
$(document).ready(function(){ 
    if($(".im-here").length > 0) 
    { 
     $(".always-here").show(); 
    } 
}); 

Oto kod Click Here!

0

tak można to zrobić przy użyciu jquery ten kod

$(document).ready(function(){ 
    if($('.im-here').length) 
     $('.always-here').show(); 
}); 

wyświetlić example here

Powiązane problemy