【发布时间】:2014-06-04 20:12:06
【问题描述】:
我刚刚了解了选项卡小部件,并制作了一个快速代码块,当单击“添加选项卡”按钮时添加一个选项卡,并在单击“删除选项卡”按钮时删除一个选项卡。我使用“Projquery”一书作为参考。不幸的是,每当单击“添加选项卡”按钮而不是仅添加一个带有内容的选项卡时,它也会将新选项卡的内容添加到所有前面的选项卡中。我检查了语法 12 次,但无济于事。肯定会得到一个答案:)
问题 - 按钮向旧选项卡添加内容和 div 文本的新选项卡,这是不应该的
<script type="text/javascript">
$(document).ready(function(){
$("#tabs").tabs();
$("button").button().click(function(e){
var tabsElem = $("#tabs");
if(this.id == "add"){
var tabID = tabsElem.children("div").length + 1;
tabsElem.children("ul").append($("<li>").append($("<a>")
.attr("href","#Tab " + tabID).text("Tab " + tabID)));
$("<div>").attr("id","Tab" + tabID).text("Cotent for given tab number "
+ tabID +", and BAM").appendTo(tabsElem);
}else{
tabsElem.children("div").first().remove();
tabsElem.find("li").first().remove();
}
tabsElem.tabs("refresh");
});
})
</script>
<body>
<div id = "tabs">
<ul>
<li><a href="#Tab1">Tab 1</a></li>
<li><a href="#Tab2">Tab 2</a></li>
<li><a href="#Tab3">Tab 3</a></li>
</ul>
<div id ="Tab1">Mad Content Don'tcha Y'know</div>
<div id = "Tab2">Welcome to the free world, you hear</div>
<div id = "Tab3">Last Time For Now</div>
</div>
<br />
<div id = "button" class = "ui-widget">
<button id = "add">Add Tab!!!</button>
<button id = "remove">Remove Tab!!!</button>
</div>
</body>
</html>
注意 - 为简洁起见,我省略了带有 jquery/css 链接的 head 标签
【问题讨论】:
标签: jquery html jquery-ui tabs