我正在创建一个网页,其中包含从数据库中提取的公告。这是我的 HTML 代码
<table width="100%" border="0">
<?php while($row = mysql_fetch_array($result)){ ?>
<tr>
<td id="maintitle" style="font-size: 34px; font-weight: bold;"><?php print $row['title']; ?></td>
</tr>
<tr>
<td id="maincontent"><?php print $row['content'];?></td>
</tr>
<tr>
<td id="mainname" style="font-size: 12px; font-style: italic;"><?php print $row['name'];?></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<?php }?>
</table>
我想要的是让 TD 元素在没有公告的情况下显示特定的文本,我决定为此使用 jQuery。这是我的代码
<script type="text/javascript">
$(document).ready(function(){
$('#maintitle',this).each(function(){
if($(this).html() != '');
$(this).append("No Incidents recorded!");
});
});
</script>
然而,即使数据库中没有公告,也没有任何反应。我想知道是否有不同的方法来解决这个问题,或者更改 jquery 代码。
最佳答案
我认为这是你想要的代码——你必须循环 tr 而不是 td
$(document).ready(function(){
$('table tr').each(function(){
if($(this).find('td').html() == ''){
$(this).find('td').html("No Incidents recorded!");
}
});
});
关于javascript - 在 jQuery 中更改 TD 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610616/