[英]JavaScript hide DIV tag if text is 0
我有个问题。 如果文本为0,我正在尝试隐藏div。我的代码是:
<script type="text/javascript">
$(function () {
if ($(".notification-counter").text() == "0") {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
</script>
<div class="dropdown nav-search pull-right <?php $this->_c('login') ?>">
<a href="../pm/" class="dropdown-toggle"><i class="fa fa-inbox"></i>
<div class="notification-container">
<div class="notification-counter">
<?php
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_uddeim_simple_notifier');
echo JModuleHelper::renderModule( $module, $attribs );
?>
</div>
</div>
</a>
</div>
但它不起作用......任何人都可以提供帮助吗? 谢谢你的回答!
尝试使用parseInt()
将数字与数字进行比较,而不是比较文本字符串(它可以缓解空白问题。JSFIDDLE
$(function () {
if (parseInt($(".notification-counter").text()) == 0) {
//$(".notification-counter").hide();
$(".notification-container").hide();
}
});
使用修剪,因为有空白
$(function () {
if ($.trim($(".notification-counter").text()) == "0") {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
只需删除0附近的引号,它就可以正常工作。
$(function () {
if ($(".notification-counter").text() == 0) {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
附加信息:由于这里的许多内容似乎不清楚,这里有一个小帮手:在您的控制台中试试这个
//hit F12 to view the console
var counter = $(".notification-counter");
var container = $(".notification-container");
console.log(container.text(), container.html());
console.log(container.text() == 0,container.text() == "0");
//true, false
console.log(typeof 0, typeof "0");
//number, string
var notificationCounter = $('.notification-counter');
if (notificationCounter.text().trim() === '0') {
notificationCounter.closest('.notification-container').hide();
}
试试这个:而不是.text()把.html()
$(function() {
if ($(".notification-counter").html() == "0") {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.