admin管理员组文章数量:1399006
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 thexhtml
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
4 Answers
Reset to default 7This 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";
};
本文标签: cssRemoving ltdivgt if Javascript is disabledStack Overflow
版权声明:本文标题:css - Removing <div> if Javascript is disabled - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744156853a2593147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论