如果div存在,Javascript显示html
我试图在我的页面上显示一个包含导航栏的表(或 ul),但只显示包含 html 上存在的名为 div 的 jquery 的选项卡。
本质上,它是一个包含所有 div 的 html 文档,jquery 隐藏除第一个 div 之外的所有 div,并且导航栏将允许浏览每个 div。 现在我试图让它更容易为我的客户使用,以便菜单项仅在其 div 也存在时才存在。我已经完成了大部分工作,唯一的事情就是实际知道 div 是否存在。
我尝试使用这个:
if(document.getElementById("page1")) {
document.write("<b>Good morning</b>");}
else
{
document.write("<b>Bad morning </b>");
}
当我将上面的代码放在 div page1 中时,它返回 true。有没有办法从页面顶部而不是在 div 内执行此操作?
谢谢!
更新:
正如许多人所建议的,我使用了以下内容:
$j(document).ready(function(){
//Hide the sections we don't need right away
$j("#page2").hide();
$j("#page3").hide();
$j("#page4").hide();
if ($j('#page1').length > 0) {
var page = 'Excellent Morning' ;
}
});
然后当我尝试使用:
document.write(page);
它显示以下内容: [对象 HTMLBodyElement]
I am trying to display a table (or ul) that will contain a navigation bar on my page, but only displays the tabs that will contain jquery called divs present on the html.
Essentially, it's a single html document that contains all divs, jquery hides all divs but the first, and the nav bar will allow to navigate through each.
Now I am trying to make it easy to use for my client, so that the menu items will only exist if the div for it also exists. I've got most of it done, the only thing is actually knowing if a div exists.
I tried using this:
if(document.getElementById("page1")) {
document.write("<b>Good morning</b>");}
else
{
document.write("<b>Bad morning </b>");
}
When I place the above code within the div page1, it returns true. Is there no way to do it from the top of the page and not within the div?
Thanks!
Update:
As suggested by many, I have used the following:
$j(document).ready(function(){
//Hide the sections we don't need right away
$j("#page2").hide();
$j("#page3").hide();
$j("#page4").hide();
if ($j('#page1').length > 0) {
var page = 'Excellent Morning' ;
}
});
Then when I try to use:
document.write(page);
It displays the following instead:
[object HTMLBodyElement]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然你已经使用了 jQuery,为什么不使用它呢?
编辑:正如 davin 指出的,你的代码应该在 DOM 渲染之后进行评估。您可以通过将其放在
$(document).ready
调用中来完成此操作:EDIT 2:根据 OP 的编辑,更好的解决方案是添加占位符元素并设置其内容(如 FishBasketGordo 建议的那样)。一个例子:
文档中的其他地方......
Why not use jQuery since you are already?
EDIT: As davin pointed out, your code should be evaluated after the DOM has been rendered. You can do this by placing it in a
$(document).ready
call:EDIT 2: Based on the OP's edits, a better solution would be to add a placeholder element and to set its content (like FishBasketGordo suggested). An example of this:
Somewhere else in the document...
如果将其放置在页面顶部,则代码运行时
page1
div 不存在。如果您使用的是 jQuery,将代码放在$(document).ready
事件中< /a>.然后,您可以将其放置在标记内您想要的位置。这是一个例子:虽然,我不会考虑使用
document.write
,而是考虑使用占位符 span 或 div,并设置它的innerHTML
属性 (或使用 jQuery 的html
方法)。我还会使用 CSS 作为我的样式,而不是标签,但这完全是另一回事。
If you place it at the top of the page, the
page1
div doesn't exist when the code runs. If you are using jQuery, place the code in a$(document).ready
event. Then, you can put it where you want it within the markup. Here's an example:Although, rather than doing a
document.write
, I would consider having a placeholder span or div, and setting it'sinnerHTML
property (or use jQuery'shtml
method). I would also use CSS for my style instead of<b>
tags, but that's another matter entirely.您可以使用
或者查看这篇文章以获得更优雅的解决方案
jQuery 是否有“exists”函数?
You can use
or you can check out this post for a more elegant solution
Is there an "exists" function for jQuery?