我有一个jQueryUI标签对象,我想要处理被点击标签的事件,当时已经选中。
使用:
$("#tabs").tabs({
select: function (event, ui) {
onSelected();
}
});
我能够在首次单击(选中)选项卡时处理该事件,但是当我再次单击它时 - 不会调用该处理程序。
那有什么解决方案吗?
如果不是 - 另一个符合我需求的选项如下:
将选项卡定义为可折叠,并按脚本折叠它们(而不是通过用户单击)。
我将tabs对象定义为:
$("#tabs").tabs({
collapsible: true
});
但是当我尝试使用以下方法模拟点击时:
$("#tabs").tabs("select", 0);
或
$("#tab-link-0").trigger('click');
没有任何反应。
我需要指出,标签是动态添加的,使用:
$("#tabs").tabs("add", "#tab-link-0", "title 0");
任何想法\任何案件的建议?
谢谢,
答案 0 :(得分:3)
我进行了一些实验,发现您可以通过调用event.preventDefault()
处理程序中的select
来阻止标签崩溃。您显然只想在当前选定的选项卡上触发select
处理程序时执行此操作(以防止折叠选项卡),或者您永远无法切换选项卡。这是值得测试的,因为在jQuery事件上调用了preventDefault()
,但它似乎可以为您提供所需的行为,至少在Chrome中是这样:
var tabs = $("#tabs").tabs({
collapsible: true,
select: function(event, ui) {
if (tabs.tabs('option', 'selected') === ui.index) {
// Prevent the tab from collapsing if the event fired on the
// currently-selected tab
event.preventDefault();
}
onSelected();
}
});