切换CSS背景颜色和图像
点击展开和折叠。 现在我想切换一个图像,它是一个CSS背景元素。 我无法弄清楚如何。
<script>
$(document).ready(function(){
$(".panel_head").click(function(){
//$(this).slideToggle("slow");
$(this).next('div').slideToggle('slow', function() {
});
var myEl = $(this);
var bgimg = myEl.css('background');
if( bgimg === #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat ) {
myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
} else {
myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
}
});
});
</script>
这是我的CSS
.panel_head {
border-radius: 0 !important;
color: white;
font-size: 14pt;
background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
text-align: center;
cursor: pointer;
}
这是我的HTML
<div class="panel custom_panel">
<div class="panel-heading panel_head" id="panel-head1">
<span class="expand-collapse-text exp" >
Categories
</span>
</div>
<div class="panel-body" id="panel-body1">
<ul class="left_options list-unstyled">
<li> <input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label> </li>
<li> <input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label> </li>
<li> <input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label> </li>
<li> <input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label> </li>
</ul>
</div>
</div>
我在脚本中收到错误。
用css代替js:
$(document).ready(function(){
$(".panel_head").click(function(){
$(this).next('div').slideToggle('slow', function() {});
$(this).toggleClass('active');
});
});
.panel_head {
border-radius: 0 !important;
color: white;
font-size: 14pt;
background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
text-align: center;
cursor: pointer;
}
.panel_head.active {
background: red url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow_1.png') 5% center no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel custom_panel">
<div class="panel-heading panel_head" id="panel-head1">
<span class="expand-collapse-text exp">
Categories
</span>
</div>
<div class="panel-body" id="panel-body1">
<ul class="left_options list-unstyled">
<li><input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label></li>
<li><input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label></li>
<li><input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label></li>
<li><input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label></li>
</ul>
</div>
</div>
嘿,你只是没有适当的解决方法(“”或“')这就是为什么
比如我给了
if( bgimg === "#00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat" ) {
//your code
}
链接地址: http://www.djcxy.com/p/74103.html
上一篇: Toggle css background color and image
下一篇: Using change() and contains() with an HTML <select> (jQuery)