使用 JavaScript 更改 CSS 类没有任何效果

发布于 2024-12-12 03:00:46 字数 1332 浏览 4 评论 0原文

我希望我的导航栏(以列表的形式)在页面加载时保持不可见(显示:无;),并在单击某些内容时出现(当前使用按钮),

这是我的 HTML 代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<script type="text/javascript" src="js/navbar.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
<script type=text/javascript>
function changeClass(){
document.getElementById("mainnav").setAttribute("class", "show");
}
</script>
</head>

<body>

<div class="wrapper">

<input  type="button" value="click" onClick="changeClass()">

<div id="mainnav">

 <ul >
<li>Home</li>
<li>Emergency Repair Service</li>
<li>Heating</li>
<li>Air Conditioning</li>
<li>Products</li>
<li>Specials</li>
<li>Contact</li>
</ul>

</div><!--mainnav-->

这是我的CSS

#mainnav {
display:none;
}

ul li {
float: left;
list-style-type: none;
}

.show {
display: block;
height: 75px;
width: 100%;
background-color: #000;
}

这是我的 javascript

function changeClass(){
document.getElementById("mainnav").setAttribute("class", "show");
}

谢谢!

I want my nav bar (that is in the form of a list) to stay invisible (display: none;) on page load, and to appear when something is clicked (currently using a button)

here is my HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<script type="text/javascript" src="js/navbar.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
<script type=text/javascript>
function changeClass(){
document.getElementById("mainnav").setAttribute("class", "show");
}
</script>
</head>

<body>

<div class="wrapper">

<input  type="button" value="click" onClick="changeClass()">

<div id="mainnav">

 <ul >
<li>Home</li>
<li>Emergency Repair Service</li>
<li>Heating</li>
<li>Air Conditioning</li>
<li>Products</li>
<li>Specials</li>
<li>Contact</li>
</ul>

</div><!--mainnav-->

Here is my CSS

#mainnav {
display:none;
}

ul li {
float: left;
list-style-type: none;
}

.show {
display: block;
height: 75px;
width: 100%;
background-color: #000;
}

Here is my javascript

function changeClass(){
document.getElementById("mainnav").setAttribute("class", "show");
}

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

或十年 2024-12-19 03:00:47

将其更改

.show {
    display: block;
    height: 75px;
    width: 100%;
    background-color: #000;
}

为:

#mainnav.show {
    display: block;
    height: 75px;
    width: 100%;
    background-color: #000;
}

.show 类中的 display:block 将覆盖 #mainnav 中的 display:none code> 选择器,因为新规则更加具体。

Change this:

.show {
    display: block;
    height: 75px;
    width: 100%;
    background-color: #000;
}

to this:

#mainnav.show {
    display: block;
    height: 75px;
    width: 100%;
    background-color: #000;
}

display:block in the .show class will then override display:none in the #mainnav selector, because the new rule is more specific.

我的痛♀有谁懂 2024-12-19 03:00:46

我认为 js 很好,但问题出在 css 中,尝试像下面这样更改它。

.show {
display: block !important;
height: 75px;
width: 100%;
background-color: #000;
}

I think js is good but problem is in css, try change it like below.

.show {
display: block !important;
height: 75px;
width: 100%;
background-color: #000;
}
胡大本事 2024-12-19 03:00:46

有用。请参阅我的示例:http://jsbin.com/uyonuc

您确定将其设置为隐藏吗最初来自CSS?

另请尝试将 !important 添加到 display:block 以确保它覆盖任何其他属性。

It works. See my example here: http://jsbin.com/uyonuc

Are you sure you're setting it to be hidden from the CSS initially?

Also try adding a !important to display:block to make sure it overrides any other property.

來不及說愛妳 2024-12-19 03:00:46

ID 选择器 (#mainnav) 规则的优先级高于类选择器 (.show) 规则。因此,即使您为 div 分配了 .show 类,display:none 仍会保留。

您最初可以使用 .hide 类,并将其替换为 .show 或在 CSS 中使用 !important 规则。

关于 CSS 特异性的文章:http://coding.smashingmagazine。 com/2010/04/07/css-特异性和继承/

ID selector (#mainnav) rules have a higher precedence than class selector (.show) rules. Therefore, display:none will persist even after you assign your div the class .show.

You could initially use a .hide class and replace it with a .show or use an !important rule in your CSS.

An article on CSS specificity: http://coding.smashingmagazine.com/2010/04/07/css-specificity-and-inheritance/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文