Tôi có một trang chứa một tập hợp các tab jQuery. Tất cả các tab trỏ vào cùng một div đích, nhưng tải nó với nội dung khác nhau thông qua ajax. Khi tôi thực hiện tải trang đầy đủ ban đầu, tôi cần đặt tab hoạt động khác nhau tùy thuộc vào các yếu tố khác nhau. Nội dung trong div mục tiêu đã được đặt trên máy chủ cho lần tải ban đầu này, vì vậy tôi không cần phải nhấp vào tab, tôi chỉ cần đặt tab chính xác thành 'đã chọn'. Tôi đã thử đặt lớp của phần tử html "li" có liên quan thành "ui-tabs-selected". Điều này có hiệu quả mong muốn ban đầu, nhưng một khi trang được tải thì khi chọn một tab khác, thẻ được chọn trước sẽ không tắt, để lại hai tab được chọn. Vì vậy, không ai biết một cách khác để chọn trước một tab (mà không gây ra nó được nhấp), hoặc một giải pháp cho hành vi "ui-tab-chọn" lạ mà tôi đang nhìn thấy.Các tab jQuery chọn tab cụ thể
Cảm ơn.
<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>
Và C# fragment đó xây dựng các UL:
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);
Một phiên bản:
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);
bạn có một mẫu mã cho thấy điều này trong hành động? – spinon
Lưu ý chắc chắn nếu đây là những gì bạn đang theo sau, nhưng đây là đoạn C# xây dựng li: nếu (currentTabCaption == kvp.Value.Caption) {active = "class = \" ui-tabs-selected \ " "; } tabsLiteral.Append ("