切换css背景颜色和图像

时间:2015-08-19 07:46:51

标签: jquery css html5 twitter-bootstrap

点击时会展开和折叠。 现在我想要与它一起切换图像,这是一个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>

我的脚本出错了。

2 个答案:

答案 0 :(得分:0)

使用css而不是js:

&#13;
&#13;
$(document).ready(function(){
  $(".panel_head").click(function(){
    $(this).next('div').slideToggle('slow', function() {});
    $(this).toggleClass('active');
  });  
});
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

嘿伙计你只是没有使用引号(&#34;&#34;或&#39;&#39;)正确解决方法这就是

例如我已经给出了

&#13;
&#13;
if( bgimg === "#00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat" ) {
  //your code
  }
&#13;
&#13;
&#13;

这是更新的jsfiddle

Updated fiddle

您可以选择以下链接

<强> How do I check if string contains substring?