jQuery隐藏一个DIV,如果它没有无序列表项 [英] jQuery to hide a DIV if it has no unordered list items

查看:105
本文介绍了jQuery隐藏一个DIV,如果它没有无序列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有无序列表项存在,我无法提供一个jQuery脚本,它可以显示div的所有内容。因此,如果div内有无序列表项,那么div将被显示,否则它将被隐藏。

I am having trouble coming up with a jQuery script that will all content of a div if there are no unordered list items present. So if there are unordered list items inside of the div then the div will be displayed, otherwise it will be hidden.

这里是基本的html,我必须测试它:

here is the basic html I have to test this:

<html>
<title>Experiment Fix Div Hidding</title
<head>

<link rel="stylesheet" type="text/css" href="css/css.css" media="all"/>
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>

</head>

<body>

<table>

<tr>
<td>
<div id="contenFirst">Sample Content. Not display if not bullets points exists
<ul>
<li><a href="#">Hello world</a></li>
<li><a href="#">Hello world2</a></li></ul></div>
</td>
</tr>

</table>

</body>
</html>

以下是我到目前为止所使用的脚本,但似乎没有job:

Here is the script I have come up with so far, but it doesn't seem to do the job:

$(document).ready(function(){

// hiding a div based on it not having any content (unordered list items)
if($("#contentFirst").html().length == 0){
$("#contentFirst").hide();
} 
});

我以正确的方式使用jquery脚本,并没有用jquery做过多的工作。

I am going in the right way with the jquery script, haven't done much with jquery before.

任何建议都将不胜感激。

Any suggestions will be greatly appreciated.

推荐答案

试试这个:

$(document).ready(function() {
    $('#contentFirst').hide();
    $('#contentFirst').has('ul').show();
});

这篇关于jQuery隐藏一个DIV,如果它没有无序列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆