[英]JavaScript hide DIV tag if text is 0
I have a problem. 我有个问题。 I'm trying to hide div if text is 0. My code is: 如果文本为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>
but it's not working... anyone can help? 但它不起作用......任何人都可以提供帮助吗? thanks for answers! 谢谢你的回答!
Try using parseInt()
to make your comparison a number vs. a number rather than comparing text strings (it alleviates issues with whitespace. JSFIDDLE 尝试使用parseInt()
将数字与数字进行比较,而不是比较文本字符串(它可以缓解空白问题。JSFIDDLE
$(function () {
if (parseInt($(".notification-counter").text()) == 0) {
//$(".notification-counter").hide();
$(".notification-container").hide();
}
});
Just remove the quotes around 0, and it would work fine. 只需删除0附近的引号,它就可以正常工作。
$(function () {
if ($(".notification-counter").text() == 0) {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
Additional Information: Since many here seem to be unclear, here's a little helper: Try this in your console 附加信息:由于这里的许多内容似乎不清楚,这里有一个小帮手:在您的控制台中试试这个
//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();
}
Try this: instead of .text() put .html() 试试这个:而不是.text()把.html()
$(function() {
if ($(".notification-counter").html() == "0") {
$(".notification-counter").hide();
$(".notification-container").hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.