最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Removing <div> if Javascript is disabled - Stack Overflow

programmeradmin4浏览0评论

I have a list that is part of a larger menu:

<ul>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
</ul>

I want to hide the list pletely IF Javascript is disabled in the web browser.

I tried one solution that works, but it uses the noscript element within head, which is not pliant with XHTML. I'm seeking a solution that plies with XHTML.

I have a list that is part of a larger menu:

<ul>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
</ul>

I want to hide the list pletely IF Javascript is disabled in the web browser.

I tried one solution that works, but it uses the noscript element within head, which is not pliant with XHTML. I'm seeking a solution that plies with XHTML.

Share Improve this question edited Jan 29, 2014 at 14:19 James Donnelly 129k35 gold badges215 silver badges223 bronze badges asked Jan 29, 2014 at 14:15 user3216933user3216933 2751 gold badge3 silver badges12 bronze badges 5
  • 2 possible duplicate of How to Show One <div> if Javascript Enabled and a Different <div> if It's Not? – Irvin Dominin Commented Jan 29, 2014 at 14:17
  • maybe use a javascript detect script and only if they have it enabled then load the menu with an ajax call with something like jquery. – azzy81 Commented Jan 29, 2014 at 14:18
  • Based on your requirements I've removed the html tag and added the xhtml tag. – James Donnelly Commented Jan 29, 2014 at 14:19
  • 2 Reverse the logic. Add items if javascript is enabled via javascript. – Nathaniel Johnson Commented Jan 29, 2014 at 14:20
  • @NathanielJohnson nice and valid trick :) – Praveen Commented Jan 29, 2014 at 14:23
Add a ment  | 

4 Answers 4

Reset to default 7

This would work

<div class="hidden">
  <ul>
    <li>Menu Item 1
    <li>Menu Item 2
    <li>Menu Item 3
  </ul>
</div>

.hidden {
  display: none;
}
$(function()
{
  $(".hidden").show();
});

You might be better going the other way and only displaying it if javascript is enabled.

Create a class something like the below...

.JSenabled {display:none}

Toggle class on $(document).ready() using $('.JSenabled').show().

Add this class to any elements you want to hide when JS is unavailable.

You'll also need to link the jQuery library.

<script src="//ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>

You can hide the ul with css, and then use javascript to show it.

In this way, the ul will not show if the javascript is disabled, because it is hidden my the css.

jQuery Way:

HTML:

<ul>
    <li>Menu Item 1</li>
    <li>Menu Item 2</li>
    <li>Menu Item 3</li>
</ul>

CSS:

ul {
    display: none;
}

jQuery:

$(document).ready(function() }
   $('ul').show();
});

When using jQuery, remember to put the jQuery libery in the <head> section of your pages:

<script src="http://code.jquery./jquery-latest.min.js"></script>

jQuery Fiddle


Plain Javascript Way:

If you don't want to use jQuery, but plain javascript you can add an ID to your <ul> and then show it with javascript like this:

HTML:

<ul id="menu">
    <li>Menu Item 1</li>
    <li>Menu Item 2</li>
    <li>Menu Item 3</li>
</ul>

CSS:

ul {
    display: none;
}

Javascript:

document.getElementById('menu').style.display = "block";

Plain Javascript Fiddle

You can add a "hidden" css id and then only show that object if javascript if enabled:

HTML:

<div class="hidden" id="hiddenDiv">
  <ul>
    <li>Menu Item 1
    <li>Menu Item 2
    <li>Menu Item 3
  </ul>
</div>

CSS:

#hiddenDiv{
  display: none;
}

Javascript

window.onload = function() {
    document.getElementById('hiddenDiv').style.display = "block";
};
发布评论

评论列表(0)

  1. 暂无评论