简体   繁体   English

如果div存在,Javascript显示html

[英]Javascript display html if div exists

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. 我试图在我的页面上显示一个包含导航栏的表(或ul),但只显示将包含html上存在的名为div的jquery的选项卡。

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. 本质上,它是一个包含所有div的单个html文档,jquery隐藏所有div,但第一个,导航栏将允许浏览每个div。 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. 现在我正在尝试使其易于用于我的客户端,以便菜单项只有在它的div也存在时才存在。 I've got most of it done, the only thing is actually knowing if a div exists. 我已经完成了大部分工作,唯一的事情就是知道div是否存在。

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. 当我将上面的代码放在div page1中时,它返回true。 Is there no way to do it from the top of the page and not within the div? 有没有办法从页面顶部而不是在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] 它显示以下内容:[object HTMLBodyElement]

Why not use jQuery since you are already? 为什么不使用jQuery,因为你已经?

if ($('#page1').length > 0) {
    // do stuff...
}

EDIT : As davin pointed out, your code should be evaluated after the DOM has been rendered. 编辑 :正如davin指出的那样,应该在渲染DOM之后评估您的代码。 You can do this by placing it in a $(document).ready call: 您可以通过将其放在$(document).ready调用中来完成此操作:

$(document.ready(function() {
    if ($('#page1').length > 0) {
        // do stuff...
    }
});

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). 编辑2 :根据OP的编辑,更好的解决方案是添加占位符元素并设置其内容(如FishBasketGordo建议)。 An example of this: 一个例子:

$(document.ready(function() {
    //Hide the sections we don't need right away
    $("#page2, #page3, #page4").hide();
    if ($('#page1').length) {
        $('#myPlaceHolder').html('<b>Good Morning</b>');
    }
    else
    {
        $('#myPlaceHolder').html('<b>Bad Morning</b>');
    }
});

Somewhere else in the document... 文件中的其他地方......

<span id="myPlaceHolder"></span>

If you place it at the top of the page, the page1 div doesn't exist when the code runs. 如果将其放在页面顶部,则代码运行时page1 div不存在。 If you are using jQuery, place the code in a $(document).ready event . 如果您使用的是jQuery, 请将代码放在$(document).ready事件中 Then, you can put it where you want it within the markup. 然后,您可以将其放在标记内的所需位置。 Here's an example: 这是一个例子:

$(document).ready(function() {
    if (document.getElementById("page1")) {
        document.write("<b>Good morning</b>");
    } else {
        document.write("<b>Bad morning </b>");
    }   
});

Although, rather than doing a document.write , I would consider having a placeholder span or div, and setting it's innerHTML property ( or use jQuery's html method ). 虽然,而不是做document.write ,我会考虑使用占位符span或div,并设置它的innerHTML属性( 或使用jQuery的html方法 )。 I would also use CSS for my style instead of <b> tags, but that's another matter entirely. 我也会使用CSS作为我的风格而不是<b>标签,但这完全是另一回事。

You can use 您可以使用

if ($(selector).length > 0) {
    // element exists
}

or you can check out this post for a more elegant solution Is there an "exists" function for jQuery? 或者你可以查看这篇文章以获得更优雅的解决方案jQuery是否存在“存在”功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM