【发布时间】:2017-12-19 18:07:03
【问题描述】:
我在一个页面上有多个按钮,单击每个按钮时,我将显示无序列表并将单击的按钮颜色更改为灰色,并将页面中的其余按钮更改为白色。下面的代码在 IE 中有效,但在 chrome/Firefox/safari 中无效。
基本上,我将 jquery 选择器用于具有类名的按钮元素,该元素基本上会将所有按钮绘制为白色,然后将单击的按钮绘制为灰色。
$('button.o365button').css('background-color', 'white !important');
我认为上述声明似乎不适用于 chrome/firefox/safari。知道为什么这不起作用。
下面是完整的工作代码
$(document).ready(function() {
$('.o365button').on('click', function(event) {
//hide all the sub menus on page loaded
$('.childHeaders').hide();
//Open the submenu that was clicked
$(this).next(".childHeaders").toggle();
//Change all button colors to white
$('button.o365button').css('background-color', 'white !important');
//Change the clicked button to grey
this.style.setProperty('background-color', '#d8d8d8', 'important');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div>
<span class="menu">
<div class="o365cs-nav-topItem">
<button class="o365button o365cs-nav-item o365cs-nav-button o365cs-topnavText ms-bgc-tdr-h" id="Products" role="menuitem" aria-haspopup="true" style="background-color: white !important;" aria-label="Opens the Settings menu" type="button"><span class="wf wf-size-x18 wf-family-o365 menustyle" role="presentation" style="font-size: 18px; vertical-align: middle; display: inline-block;">Products</span></button>
<div class="childHeaders" style="display:none;">
<ul class="subnav" style="width: 720px;">
<ul id="Sco5">
<li id="5"><a style="font-weight: bold;">Scorecards/Dashboards</a></li>
</ul>
</div>
</div>
<div class="o365cs-nav-topItem">
<button class="o365button o365cs-nav-item o365cs-nav-button o365cs-topnavText ms-bgc-tdr-h" id="Support" role="menuitem" aria-haspopup="true" style="background-color: white !important;" aria-label="Opens the Settings menu" type="button"><span class="wf wf-size-x18 wf-family-o365 menustyle" role="presentation" style="font-size: 18px; vertical-align: middle; display: inline-block;">Support</span></button>
<div class="childHeaders" style="display:none;">
<ul class="subnav">
<ul id="Use31">
<li id="31"><a href="https://unicef-insight1.uservoice.com/" target="_blank">User Support Portal</a>
</ul>
</div>
</div>
</span>
</div>
IE
火狐/Chrome
【问题讨论】:
-
您是否尝试过清除缓存?试试 ctrl+f5
-
当我为您制作 sn-p 时,您可以从格式化程序问题中看到,HTML 无效 - 您不能在 span 中有 div
-
你为什么使用两种不同的方式来改变 CSS。只需切换你的白色
-
是的,我的缓存已被清除,但结果没有变化。
-
你也不能有一个
<ul>作为另一个<ul>的孩子