[英]How can I hide a Div when text of a label is empty?
我試圖通過檢查標簽是否為空來隱藏'div'。 但是出了點問題。 下面是我使用的代碼。
<script>
function myfun(){
if (document.getElementById('department').innerHTML=='') {
document.getElementById('departmentdiv').style.display="none";
}
}
</script>
<body onload="myfun()">
<div id="departmentdiv" class="row-fluid">
<label>DEPARTMENT:</label>
<label id="department"><?php if (!empty($_POST['department']))echo $_POST['department'];?></label>
</div>
</body>
我從另一個頁面獲取POST [部門]值,並將其分配給該頁面中的標簽。 我只想在值不為null時顯示div。 但是,問題是即使此標簽不為null,標簽也不會在此頁面上顯示。 有人可以幫我嗎
可能是您可以嘗試使用php空函數進行驗證。如果郵政部門不為空,則顯示:
<script>
function myfun(){
<?php if (empty($_POST['department'])){?>
document.getElementById('departmentdiv').style.display="none";
<?php }?>
}
</script>
嘗試這樣的事情,
if (document.getElementById('department').innerHTML.length) {
document.getElementById('departmentdiv').style.display="none";
}
如果空格引起了問題,
if (document.getElementById('department').innerHTML.trim().length) {
document.getElementById('departmentdiv').style.display="none";
}
使用isset()
代替empty()
:)
InnerHTML運行正常:
if (document.getElementById('department').innerHTML == '') {
document.getElementById('departmentdiv').style.display = "none";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.