[英]Hiding a div when the innerHTML is null
I am pretty new to jQuery but I am trying to get a code setup which hides a div when the 'innerHTML' is null. 我是jQuery的新手,但我试图得到一个代码设置,当'innerHTML'为null时隐藏div。 I tried using the code below. 我尝试使用下面的代码。 But it doesn't work! 但它不起作用! Where is my fault?? 我的错在哪里?
if (($("#php-errors").html).length) {
$("#php-errors").css("display", "block");
}
else {
$("#php-errors").css("display", "none");
}
if ($("#php-errors").html().length) {
$("#php-errors").css("display", "block");
}
else {
$("#php-errors").css("display", "none");
}
Correction: 更正:
($("#php-errors").html).length
should be $("#php-errors").html().length
($("#php-errors").html).length
应为$("#php-errors").html().length
One line, and using .show()
and .hide()
methods: 一行,并使用.show()
和.show()
.hide()
方法:
var hasCont = $("#php-errors").contents().length ? $("#php-errors").show() : $("#php-errors").hide();
Using the ternary operator that says: 使用三元运算符说:
var $el
, and use it like:
一个好的做法是将你的元素缓存在var中,让我们称之为var $el
,并使用它:
var $el = $("#php-errors"); var hasCont = $el.contents().length ? $el.show() : $el.hide();
Much more readable, and it will save you some micro processing time ;) but it really helps in terms of cross-function reusability (if defined outside the function.) 更具可读性,它将为您节省一些微处理时间;)但它确实有助于跨功能可重用性(如果在函数外定义)。
You can clean your code a little like this. 您可以像这样清理代码。
It also uses the innerHTML
property as the condition part of the ternary expression. 它还使用innerHTML
属性作为三元表达式的条件部分。 If there's any content, it'll return and set "block", if not, then "none". 如果有任何内容,它将返回并设置“阻止”,如果没有,则返回“无”。
$("#php-errors").css("display", function() {
return this.innerHTML ? "block" : "none";
});
http://jsfiddle.net/WKWNc/2/ http://jsfiddle.net/WKWNc/2/
update: 更新:
If this only runs when the page loads, you could initially have it set to "block", and then do this. 如果这仅在页面加载时运行,则最初可以将其设置为“阻止”,然后执行此操作。
$("#php-errors:empty").hide();
http://jsfiddle.net/WKWNc/1/ http://jsfiddle.net/WKWNc/1/
Or the opposite, have it set to "none", and show it if not empty. 或者相反,将它设置为“none”,并显示它是否为空。
$("#php-errors:not(:empty)").show();
Just to add the css way that will not require javascript in case someone else needs it: 只是为了添加不需要javascript的css方式,以防其他人需要它:
.php-errors:empty { display: none; }
Will Match: 将匹配:
<div class="php-errors"></div>
<div class="php-errors"><!-- test --></div>
Will Not Match: 不匹配:
<div class="php-errors"> </div>
<div class="php-errors">
<!-- test -->
</div>
<div class="php-errors">
</div>
Source: https://css-tricks.com/almanac/selectors/e/empty/ 资料来源: https : //css-tricks.com/almanac/selectors/e/empty/
For your case, it seems you just want: 对于你的情况,似乎你只是想:
$("#php-errors").toggle();
I can see no case where you want to set the display to either block
or none
if none is already set. 我可以看到你想要的显示设置为任何情况下, block
或none
,如果没有已经设置。
Are you sure your logic isn't redundant? 你确定你的逻辑不是多余的吗?
I think u missed compare , try this 我想你错过了比较,试试这个
if ($("#php-errors").html() == "") {
$("#php-errors").css("display", "block");
}
else {
$("#php-errors").css("display", "none");
}
I would do something like this: 我会做这样的事情:
var $php_errors = $("#php-errors");
if ($php_errors.is(":empty")) {
$php_errors.hide()
}
else {
$php_errors.show();
}
hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.