Tôi chưa sử dụng jQuery Boilerplate, nhưng tôi tin rằng vấn đề ở đây là với biến số của bạn có tên là plugin
.
Không có mã nào trong mã của bạn, bạn khai báo biến có tên là plugin
. Khi tôi dừng trình gỡ rối trong Plugin.prototype.showTabContent
, tôi có thể đánh giá window.plugin
và nó trả về giá trị toàn cầu cho plugin.
Trong hàm tạo cho Plugin, dòng đầu tiên đọc plugin= this;
. Vì plugin
không được xác định, nó khai báo biến ở phạm vi toàn cục trên đối tượng window
.
Khắc phục là chuyển một tham chiếu đến đối tượng plugin
khi thiết lập móc $().on()
. Dữ liệu được truyền có sẵn trong trình xử lý sự kiện thông qua tham số event
được truyền trong thuộc tính data
.
Dưới đây là giải pháp (tại http://codepen.io/shhQuiet/pen/JXEjMV)
(function($, window, document, undefined) {
var pluginName = "tabs2Accordion",
defaults = {
menuSelector: ".tabs2accordion-menu",
tabContentSelector: ".tabs2accordion-content"
};
function Plugin(element, options) {
this.element = element;
this.$element = $(this.element);
this.options = $.extend({}, defaults, options);
this.$menu = $(this.element).find(this.options.menuSelector),
this.$tabs = $(this.element).find(this.options.tabContentSelector),
this.$accordionTriggers = $(this.element).find(this.$tabs).find("h3");
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
//Set all the tab states to inactive
this.$tabs.attr("data-active", false);
//Set the first tab to active
this.$tabs.first().attr("data-active", true);
//If you click on a tab, show the corresponding content
this.$menu.on("click", "li", this, this.showTabContent);
//Set the dimensions (height) of the plugin
this.resizeTabs2Accordion({
data: this
});
//If the browser resizes, adjust the dimensions (height) of the plugin
$(window).on("resize", this, this.resizeTabs2Accordion);
//Add a loaded class to the plugin which will fade in the plugin's content
this.$element.addClass("loaded");
console.log(this.$element);
},
resizeTabs2Accordion: function(event) {
var contentHeight;
var plugin = event.data;
if (!plugin.$element.is("[data-nested-menu]")) {
contentHeight = plugin.$tabs.filter("[data-active='true']").outerHeight() + plugin.$menu.outerHeight();
} else {
contentHeight = plugin.$tabs.filter("[data-active='true']").outerHeight();
}
plugin.$element.outerHeight(contentHeight);
},
showTabContent: function(event) {
var $target;
var plugin = event.data;
plugin.$menu.children().find("a").filter("[data-active='true']").attr("data-active", false);
plugin.$tabs.filter("[data-active='true']").attr("data-active", false);
$target = $($(this).children("a").attr("href"));
$(this).children("a").attr("data-active", true);
$target.attr("data-active", true);
plugin.resizeTabs2Accordion({data: plugin});
return false;
},
showAccordionContent: function(event) {
var plugin = event.data;
$("[data-active-mobile]").not($(this).parent()).attr("data-active-mobile", false);
if ($(this).parent().attr("data-active-mobile") === "false") {
$(this).parent().attr("data-active-mobile", true);
} else {
$(this).parent().attr("data-active-mobile", false);
}
}
};
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);
$(window).on("load", function() {
$(".tabs2accordion").tabs2Accordion({
state: "desktop"
});
});
* nhưng bắt đầu trục trặc ngay khi phần tử khác có cùng lớp được thêm vào trang * .... Làm thế nào? bạn chỉ mơ hồ giải thích mã của bạn * nên làm gì, và havent giải thích tất cả những gì vấn đề thực tế là khác mà để nói có một vấn đề. Vui lòng cụ thể hơn – DelightedD0D
Trên kích thước màn hình 768px trở lên, việc nhấp vào một trong ba tiêu đề sẽ hiển thị đoạn nội dung tab tương ứng. Nhấp vào một tiêu đề khác sẽ hiển thị một đoạn nội dung tab khác. Tôi có thể đã không mô tả nó rất tốt nhưng tôi đã cung cấp một Codepen chứng minh vấn đề vì vậy không có lý do để thô lỗ –
Lời xin lỗi của tôi cho đến thô lỗ, tôi chỉ muốn bạn thấy rằng bạn havent cho chúng tôi những thông tin chúng tôi cần để giúp bạn . Chúng tôi rất sẵn lòng trợ giúp, nhưng trước khi chúng tôi đi qua mã trong ví dụ của bạn, chúng tôi cần biết mã * nên làm gì * và * cách mã hiện không thành công * .đây không phải là thường lệ đối với người ngoài vì chúng cho người hỏi. Ví dụ, khi tôi làm cho màn hình nhỏ hơn trong bản demo của bạn, không có gì xảy ra khi tôi nhấp vào tiêu đề, xem http://i.imgur.com/v0JPO2g.png – DelightedD0D