編輯:我想我得到了解決方案!在我要求進一步幫助之前,我想自己嘗試修復此問題=)單擊同一元素上兩個腳本的事件?
第一個腳本禁止第二個腳本作爲來自第一個腳本的click事件覆蓋第二個腳本。因爲第二個不起作用,所以不可能打開下拉菜單來選擇一個列表項來觸發第一個腳本點擊。
我試着用event.stopPropagation()替換所有返回的錯誤語句。但沒有工作。試圖重新排序我的腳本,但也失敗了。我正在考慮讓我的第二個腳本以另一個父div爲目標,但那也沒用。我也嘗試了event.stopImmediatePropagation()和.bind方法。
有什麼想法?
第一個使下拉功能的腳本。包含點擊事件。
function DropDown(el) {
this.f = el;
this.placeholder = this.f.children('span');
this.opts = this.f.find('ul.dropdown > li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.f.on('click', function(event){
$(this).toggleClass('active');
return false;
});
obj.opts.on('click',function(){
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
obj.placeholder.text(obj.val);
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
}
$(function() {
var f = new DropDown($('#f'));
$(document).click(function() {
// all dropdowns
$('.filter-buttons').removeClass('active');
});
});
,做了過濾的第二腳本,還包含點擊事件:
jQuery(document).ready(function(e) {
var t = $(".filter-container");
t.imagesLoaded(function() {
t.isotope({
itemSelector: "figure",
filter: "*",
resizable: false,
animationEngine: "jquery"
})
});
$(".filter-buttons a").click(function(evt) {
var n = $(this).parents(".filter-buttons");
n.find(".selected").removeClass("selected");
$(this).addClass("selected");
var r = $(this).attr("data-filter");
t.isotope({
filter: r
});
evt.preventDefault();
});
$(window).resize(function() {
var n = $(window).width();
t.isotope("reLayout")
}).trigger("resize")
});
HTML結構
<div id="f" class="filter-buttons" tabindex="1">
<span>Choose Genre</span>
<ul class="dropdown">
<li><a href="#" data-filter="*" class="selected">All</a></li>
<li><a href="#" data-filter=".electronic">Electronic</a></li>
<li><a href="#" data-filter=".popular">Popular</a></a></li>
</ul>
</div>
@Aiden Kane我實際上傳了一個測試網站,以便您可以更好地瞭解發生了什麼:http://rolfvohs.com/ – DeanDemona 2014-10-11 15:36:49
已添加html結構以獲取更多信息 – DeanDemona 2014-10-11 15:38:23
因此,正如我所描述的那樣。你有一個事件處理程序附加到頂層節點('#f'),另一個更深入'$(「。filter-buttons a」)。click'。讓我在答案中添加細節。 – 2014-10-11 15:42:35