[英]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.