如果.active类,JQuery不会删除背景图像

时间:2013-10-27 20:22:38

标签: jquery css

我遇到了jquery无法删除背景图片的问题。

我正在使用带有深层链接的Zurb's Foundation framework,并且在此表单中有一些代码(我已经删除了相当多的HTML):

<div id="contentWrap">
  <section id="one" class="mixed">
    <a href="#">One</a>
    <div class="content"></div>
  </section>   
  <section id="two" class="mixed active">
    <a href="#">One</a>
    <div class="content"></div>
  </section>
</div>

我正在使用css将#contentWrap设置为背景图像,但我只希望在第一个链接处于活动状态时显示图像。

我正在使用这个JQuery代码:

// Add background image if #one clicked
$('#one a').click(function(){
  $("#contentWrap").css("background-image","url('xx.jpg')");
});

// Remove background image if #two clicked
$('#two a').click(function(){
  $("#contentWrap").css("background-image","none");
});

// Remove background image if #two has class of .active
if ( $("#two").hasClass("active") ) {
  $("#contentWrap").css("background-image","none")
};

如果通过深层链接(即xx.com/#two)激活第二个标签页面,则仍会加载背景图像。

我以前有这个工作,但后来对(wordpress)网站上加载代码的方式进行了几次更新,现在使用jQuery(document).ready(function($)来包装所有其他JS(正在工作)。 / p>

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:1)

如果我说得对,当two div处于活动状态时,才会删除背景?

// Remove background image if #two clicked
$('#two a').click(function(){
   // Remove background image if #two has class of .active
    if ( $("#two").hasClass("active") ) {
      $("#contentWrap").css("background-image","none")
    };
});

工作jsfiddle :(将background-image更改为backgroundhttp://jsfiddle.net/Vvg5F/4/