根据 div 内的内容显示/隐藏
我想根据包含内容或不包含内容的 div 标签显示或隐藏标题元素(例如 )。内容是动态添加的,但 header 元素是静态的,这会导致 header 元素多次显示而其下方没有内容。这是一个示例:
<div id=related-section>
<h3>Related Articles</h3>
<div id=related>
<p>This content is being shown, sometimes it is not shown.</p>
</div>
</div>
更多详细信息:
I would like to show or hide a header element (e.g. <h3></h3>
) based up a div tag that either contains content or not. The content is dynamically added, but the header element is static, which results in having the header element shown with no content below it many times. Here's an example:
<div id=related-section>
<h3>Related Articles</h3>
<div id=related>
<p>This content is being shown, sometimes it is not shown.</p>
</div>
</div>
Further details:
There are two states for what <div id=related>
looks like, it's either (for example) 1) <div id=related><p>Prima insolens hendrerit his et, sit exerci detracto...</p></div>
or 2) <div id=related></div>
. The latter showing that there's zero content inside the div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jQuery,在更新
#lated
的内容后,您可以执行以下操作,但如果它是您提供的完整代码,您还可以隐藏/显示整个 div
#lated-section
> 因为此时它没有其他可见内容。Using jQuery you could do the following, after updating the contents of
#related
But if it is the full code you provided you could also hide / show the whole div
#related-section
because it has no other visible content then.使用此处的代码为您创建了一个小提琴: http://jsfiddle.net/2vJfN/
Created a fiddle for you with the code here: http://jsfiddle.net/2vJfN/