繁体   English   中英

jQuery,仅在显示其他内容时显示

[英]jquery, only show if something else is showing

有没有一种方法可以检查是否不存在某些东西,即PHP尚未将其打印出来,只有在不存在时,该信息才会出现?

$(document).ready(function() {
$('#info').hide();
//can i check over here if something with an id of 'space' exists in the document flow, and if it doesn't, then execute the next line?
$('#info').show("slide", { direction: "right" }, 400);
});

要检查元素是否存在,可以使用以下命令

if ($('#elementID').length) {
   ...
}

要检查是否不存在,您可以执行

if ($('#elementID').length == 0) {
   ...
}

要么

if (!$('#elementID').length) {
   ...
}

所以你可以

$(document).ready(function() {
    $('#info').hide();
    if ($('#space').length == 0) {
        $('#info').show("slide", { direction: "right" }, 400);
    }
});
if( !$('#space') ){
  $('#info').show("slide", { direction: "right" }, 400);
}

保持简单。 您不需要.length。

您甚至可以通过php使用它,例如:

<?php if(CONDITION): ?>
Content to display
<?php endif; ?>

特别是它可能变成

<?php if($something != "" || Other conditions): ?>
content to display
<?php endif; ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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