我正在尝试制作一个基本上是 Kendo tabstrip 的网络部件。它自己有一个简单的 ul,并使用 JavaScript 代码将外部 html 文件链接到每个相关的 li。到目前为止效果很好。 但现在我希望能够调用函数来添加或删除我在选项卡中选择的任何文件,但到目前为止它还不起作用。
我搜索并找到了一些适合这项工作的功能,但这个更接近我的想法。 当我使用添加按钮时,选项卡条已创建,但 contecturl 链接不起作用,它只是一个空选项卡。
<------------------------ web-part ------------------------>
<div class="row">
<input type='text' id='tabname' name='tabname'>
<input type='text' id='tabLink' name='tabLink'>
<input type="button" value="Add Tab" onclick="AddTab()" />
<input type="button" value="Remove Tab" onclick="closeTab()" />
</div>
<div id="tabstrip">
<ul id="tabStripUL">
<li class="k-state-active">tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<------------------------ Javascript ------------------------>
$(document).ready(function () {
InitLoadTabstrip();
});
function InitLoadTabstrip() {
var ts = $("#tabstrip").kendoTabStrip({
animation: { open: { effects: "fadeIn" } },
select: function(element){selecttab(element)},
contentUrls: [
'Page1.html',
'Page2.html',
'Page3.html',
]
}).data('kendoTabStrip');
}
function selecttab(element) {
var tabStrip1 = $('#tabstrip').kendoTabStrip().data("kendoTabStrip");
var item = tabStrip1.element.find("li:contains("+$(element.item).text()+")"),
itemIdx = item.index();
$("#tabstrip").data("kendoTabStrip").select(itemIdx);
}
function AddTab() {
var title = jQuery("#tabname").val();
var Address = jQuery("#tabLink").val();
var tabStrip = $("#tabstrip").kendoTabStrip().data("kendoTabStrip");
tabStrip.append({
text: title,
contentUrl: Address
});
tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
}
function closeTab() {
var tabStrip = $('#tabstrip').kendoTabStrip().data("kendoTabStrip");
tabStrip.remove(tabStrip.select());
tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
}
它应该获取名称和地址,并根据按钮将该选项卡添加到选项卡条或将其删除。 如果有人能提供帮助,我将不胜感激。
<---------------------------- 快速更新 -------------- -------------->
我尝试删除按钮并简单地向 addTab 函数添加一个参数来添加调用的每个页面。像这样的东西:
function addTab(tabName) {
var tabStrip = $("#tabstrip").kendoTabStrip().data("kendoTabStrip");
if (tabName == "name1") {
tabStrip.append({
text: "title1",
contentUrl: 'page1.html',
});
}
else if (tabName == "name2") {
tabStrip.append({
text: "title2",
contentUrl: 'page2.html',
});
}
tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
}
并这样调用它们:
$(document).ready(function () {
InitLoadTabstrip();
});
function InitLoadTabstrip() {
var ts = $("#tabstrip").kendoTabStrip({
animation: { open: { effects: "fadeIn" } },
select: function(element){selecttab(element)},
contentUrls: [
]
}).data('kendoTabStrip');
addTab("name1");
addTab("name2");
}
现在的问题是,当我尝试添加多个选项卡时,一个接一个(如代码),tabstrip 将两个列表项设置为事件状态,并且它会破坏 tabstrip。我认为这可能是因为 'tabstrip.select' ,但我不太明白出了什么问题。
最佳答案
所以我设法自己解决了这个问题,认为这可能会对其他人有所帮助。 问题是,附加后,我有多个带有“k-state-active”类的列表项,这破坏了我的 tabstrip 。我使用 jquery 手动删除事件类(无论它们在哪里)并将其添加到第一个 li 。 另外,我每次调用 addTab() 时都会创建一个新变量,而不是处理同一个变量,这使得整个过程变慢并且没有动画和选择。所以我将“ts”公开以在所有函数中使用。 所以最终的代码是这样的:
<---------------HTML------------------>
<div id="tabstrip" style="width: 100%">
<ul id="tabStripUL">
</ul>
</div>
<----------------Script--------------->
var ts;
$(document).ready(function () {
InitLoadTabstrip();
});
function InitLoadTabstrip() {
ts = $("#tabstrip").kendoTabStrip({
animation: { open: { duration: 150, effects:"fadeIn" }
},
select: function(element){selecttab(element)},
contentUrls: [
]
}).data('kendoTabStrip');
addTab("tab1");
addTab("tab2");
}
//ts couldn't work on selecttab because of call limited size (don't really know what it is)
function selecttab(element) {
var tabStrip1 = $('#tabstrip').kendoTabStrip().data("kendoTabStrip");
var item = tabStrip1.element.find("li:contains("+$(element.item).text()+")"),
itemIdx = item.index();
$("#tabstrip").data("kendoTabStrip").select(itemIdx);
}
function addTab(tabSelect) {
if (tabSelect == "tab1") {
ts.append({
text: "title1",
contentUrl: 'page1.html',
});
//sets an id to each tab
ts.tabGroup.children().last().attr("id", "tab1");
}
else if (tabSelect == "tab2") {
ts.append({
text: "title2",
contentUrl: 'page2',
});
ts.tabGroup.children().last().attr("id", "tab2");
}
ts.select((ts.tabGroup.children("li").length - 1));
$("#tabstrip li").find(".k-state-active").removeClass("k-state-active k-tab-on-top");
$("#tabstrip li:first").addClass("k-state-active k-tab-on-top");
}
// ClearTS: clears all the tabs and contents
function clearTS() {
$("#tabstrip li").remove();
$("#tabstrip .k-content").remove();
希望对你有帮助!
关于javascript - 将 HTML 文件作为选项卡动态添加到 Kendo TabStrip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723400/