2015-11-07 95 views
1

我有一个页面,用户通过点击一个电子书到达。当他们点击他们花一个链接到一个产品页面上取决于链接,所以它看起来像更改当前活动div内的div的CSS(页面加载)

example.com/#product1

example.com/#product6

例子。 COM /#product15,

等等

页面已经滚动到使用此代码的页面的一部分:

<script type="text/javascript"> 
jQuery(document).ready(function(){ 
    jQuery('html,body').animate({ 
      scrollTop: jQuery(window.location.hash).offset().top-150 
     }, 900, 'swing'); 
}); 

我有#product1 DIV内的另一个DIV叫#amazonbuttondisplay:none风格。

<div id="product1" class="product"> 
    <a href="link"> 
    <div id="amazonbutton"><img src="amazonbuttonz" /> 
    </div> 
    </a> 
</div> 

这是因为它是一个“去亚马逊”按钮隐藏除当前选定的div以外的所有项目。所以基本上,这个按钮应该显示:active div,同时保持其原始状态(隐藏),用于所有不活动的div。

我该如何改变这与jquery?

我的代码是不完整的这一点,我很卡:

jQuery(function() { 
    jQuery('amazonbutton').each(function() { 
    if (jQuery(this).attr('href') === window.location.href) { 
     jQuery(this).addClass('active'); 
    } 
    }); 
}); 

预先感谢您!

+0

如果您可以在此创建类似的演示程序,或者使用JSFiddle,则更好。 – divy3993

+0

你能简单而明确地解释你想要达到的目标和问题吗? –

回答

1

有一件很酷的事情叫document.activeElement。它是跟踪当前活动元素的只读属性。

$('.yourdivclass').hide(); 
$(document.activeElement).show(); 

您可以通过.focus().activate()

1

首先激活元素,你的循环amazonbutton,在你的代码,我看到了,这是不是一个链接。所以你不能检查attr href。我想你想检查amazonbutton的父母的href。其次,你的循环中的amazonbutton是一个类或ID?您必须将.#添加到它。代码可能是:

jQuery(function() { 
    jQuery('#amazonbutton').each(function() { 
    if (jQuery(this).parent().attr('href') === window.location.href) { 
     jQuery(this).addClass('active'); 
    } 
    }); 
});