1

I have a search page with one div on it (#A), when I do a search, I have made another div appear on the page (#B), however I don't ever want the two divs on the page at the same time.

How can I code it so that if #B exists, #A is hidden? Do I use the jQuery .length attribute??

4

3 回答 3

3

可以使用jQuery来检查div的css状态,即是display:none还是display:block

var aVisible = $('#divA').css('display');
if (aVisible == 'block') {
//Put your code here to hide DivA
}
于 2013-06-04T15:43:46.470 回答
3

您可以使用length

//if B exist...
if($('#B').length){
   $('#A').hide();
}

活生生的例子:http: //jsfiddle.net/4P9Hq/

于 2013-06-04T15:42:25.803 回答
2

您可以toggle()通过将 abool作为参数传递给元素。这与检查元素是否有效相结合:hidden

jsFiddle

$('#A').toggle($('#B').is(':hidden'));

检查存在length

jsFiddle

$('#A').toggle(!$('#B').length);
于 2013-06-04T15:43:12.420 回答