我有以下代码(on jsfiddle here)
$(function(){
var $container = $('#gallery');
$container.isotope({
filter: '*',
animationOptions: {
duration: 750,
easing: 'linear',
queue: false
}
});
var $optionSets = $('ul.nav'),
$optionLinks = $optionSets.find('a');
$optionLinks.click(function () {
var $this = $(this);
if ($this.hasClass('selected')) {
return false;
}
var $optionSet = $this.parents('ul.nav');
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
});
// HASH HISTORY WITH JQUERY BBQ
$('ul.nav a').click(function () {
// get href attr, remove leading #
var href = $(this).attr('href').replace(/^#/, ''),
// convert href into object
// i.e. 'filter=.inner-transition' -> { filter: '.inner-transition' }
option = $.deparam(href, true);
// set hash, triggers hashchange on window
$.bbq.pushState(option);
return false;
});
//just a function to quickly add and remove .selected
function changeSelectedLink($elem) {
$elem.addClass('selected');
}
$(window).bind('hashchange', function (event) {
//checks if there is a hash in the url and puts hashes in hashOptions
$(".selected").removeClass("selected");
var hashOptions = window.location.hash ? $.deparam.fragment(window.location.hash, true) : {}, options = $.extend({}, hashOptions);
$('#gallery').isotope(options);
var hrefObj, hrefValue, $selectedLink;
//go over each hashOption and convert it to a variable
for (var key in options) {
hrefObj = {};
hrefObj[key] = options[key];
hrefValue = $.param(hrefObj);
$selectedLink = $('ul.nav').find('a[href="#' + hrefValue + '"]');
changeSelectedLink($selectedLink);
}
}).trigger('hashchange'); //this continues the hashchange event
});
此代码适用于chrome。但是在firefox 22和10中它表现得很奇怪
点击颜色时一切正常。返回时,代码应该以清除.selected
的方式运行,并且只添加到正确的节点。结果是在DOM中清除.selected
(如果我检查元素),但在屏幕上却没有。单击屏幕上的任意位置后,该类将被删除。
此外,如果我用firebug调试等,这不会发生!
我在代码中遗漏了什么吗?
答案 0 :(得分:1)
删除css中的a:focus
或将blur
添加到$('.selected').removeClass('selected').blur();
中的hashchange
只有一个.selected