gpt4 book ai didi

jQuery 选项卡选择特定选项卡

转载 作者:行者123 更新时间:2023-12-03 22:34:41 26 4
gpt4 key购买 nike

我有一个包含一组 jQuery 选项卡的页面。所有选项卡都指向同一个目标 div,但通过 ajax 加载不同的内容。当我执行初始整页加载时,我需要根据各种因素以不同方式设置事件选项卡。目标 div 中的内容已在服务器上设置用于此初始加载,因此我不需要单击选项卡,只需将正确的选项卡设置为“选定”即可。我尝试将相关“li”html 元素的类设置为“ui-tabs-selected”。这达到了最初所需的效果,但是一旦加载页面,然后在选择另一个选项卡时,预选的选项卡不会关闭,而是选择两个选项卡。

那么,有谁知道预选择选项卡的替代方法(不会导致其被单击),或者解决我所看到的奇怪的“ui-tabs-selected”行为。

谢谢。

<script type="text/javascript">
$(function() {
$("#panelTabs").tabs({
ajaxOptions: {
error: function(xhr, status, index, anchor) {
$(anchor.hash).html("Couldn't load this tab. We'll try to fix this as soon as possible.");
}
}
});

$("#panelTabs").tabs({
select: function(event, ui) {
getPage('hps.aspx?cmd=zz_' + ui.tab.id, 'panelA');
}
});
});
</script>

以及构建 UL 的 C# 片段:

StringBuilder tabsLiteral = new StringBuilder();
tabsLiteral.Append("<ul>");
foreach (KeyValuePair<string, Tab> kvp in tabs)
{
tabsLiteral.Append("<li>");
// Note - the kvp.Value.URI determines what should happen when the Tab is clicked
tabsLiteral.Append("<a id=\"" + kvp.Value.URI + "\" href=\"#panelTabs\">" + kvp.Value.Caption + "</a>");
tabsLiteral.Append("</li>");
}
tabsLiteral.Append("</ul>");
_panelTabs.Controls.Add(new LiteralControl(tabsLiteral.ToString()));

HtmlGenericControl ctl = new HtmlGenericControl();
StringBuilder html = new StringBuilder();
html.Append("<script type=\"text/javascript\">");
html.Append("$(\"#panelTabs\").tabs({selected: 2});");
html.Append("</script>");
ctl.InnerHtml = html.ToString();
_panelTabs.Controls.Add(ctl);

另一个版本:

StringBuilder tabsLiteral = new StringBuilder();
tabsLiteral.Append("<ul>");
foreach (KeyValuePair<string, Tab> kvp in tabs)
{
string active = "";
if (currentTabCaption == kvp.Value.Caption)
{
//active = " class=\"ui-tabs-selected\"";
}
tabsLiteral.Append("<li" + active + ">");
// Note - the kvp.Value.URI determines what should happen when the Tab is clicked
tabsLiteral.Append("<a id=\"" + kvp.Value.URI + "\" href=\"#panelTabs\">" + kvp.Value.Caption + "</a>");
tabsLiteral.Append("</li>");
}
tabsLiteral.Append("</ul>");
_panelTabs.Controls.Add(new LiteralControl(tabsLiteral.ToString()));

HtmlGenericControl ctl = new HtmlGenericControl();
StringBuilder html = new StringBuilder();
html.Append("<script type=\"text/javascript\">");
//html.Append("$(\"#panelTabs\").tabs('option','selected', 2);");
html.Append(@"$(function() {
alert('initialising tabs');
$(""#panelTabs"").tabs({
ajaxOptions: {
error: function(xhr, status, index, anchor) {
$(anchor.hash).html(""Couldn't load this tab. We'll try to fix this as soon as possible."");
}
},
select: function(event, ui) {
getPage('hps.aspx?cmd=zz_' + ui.tab.id, 'panelA');
}
});
});");
html.Append("$(\"#panelTabs\").tabs({selected: 2});");
html.Append("</script>");
ctl.InnerHtml = html.ToString();
//_panelTabs.Controls.Add(ctl);
Page.Controls.Add(ctl);

最佳答案

您正在寻找的是 selected option 。例如

$("#MyTabs").tabs({
selected: 2
});

关于jQuery 选项卡选择特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3162148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com