1
我是jQuery的新手,我需要進一步瞭解一個讓我卡住了一段時間的主題。 我正在開發一個應用程序,其中使用HTML5 + Javascript生成GUI前端,我需要處理一個字符串(來自用戶)並在運行中修改它們的選項卡和數量。 (即使創建和銷燬標籤)。從javascript函數添加刪除jQuery選項卡
爲標籤的代碼是從JQueryUI挑選:
<script>
$(function() {
var tabTitle = $("#tab_title"),
tabContent = $("#tab_content"),
tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
tabCounter = 2;
var tabs = $("#tabs").tabs();
// modal dialog init: custom buttons and a "close" callback resetting the form inside
var dialog = $("#dialog").dialog(
{
autoOpen: false,
modal: true,
buttons: {
Add: function()
{
addTab();
$(this).dialog("close");
},
Cancel: function()
{
$(this).dialog("close");
}
},
close: function() {
form[ 0 ].reset();
}
}
);
// addTab form: calls addTab function on submit and closes the dialog
var form = dialog.find("form").submit(function(event)
{
addTab();
dialog.dialog("close");
event.preventDefault();
});
// actual addTab function: adds new tab using the input from the form above
function addTab()
{
var label = tabTitle.val() || "Tab " + tabCounter,
id = "tabs-" + tabCounter,
li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)),
tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";
tabs.find(".ui-tabs-nav").append(li);
tabs.append("<div id='" + id + "'><p>" + tabContentHtml + "</p></div>");
tabs.tabs("refresh");
tabCounter++;
}
// addTab button: just opens the dialog
$("#add_tab").button().click(function() {dialog.dialog("open");});
// close icon: removing the tab on click
tabs.delegate("span.ui-icon-close", "click", function() {var panelId = $(this).closest("li").remove().attr("aria-controls");
$("#" + panelId).remove();
tabs.tabs("refresh");});
});
function test()
{
/*
TODO: Generate the tab title and content and update the GUI with the new tab, generated
only by writing it in the code.
*/
}
</script>
我已經工作了很多關於這個問題,但我不能找到一種方法,從功能測試()在我的腳本中定義添加的標籤。我如何訪問原始代碼中提供的功能?
謝謝!
我很欣賞你的答案真多!這將是可以訪問這種情況下(我猜$(函數() {})從另一個函數,我需要在全球範圍內可用?這是因爲我需要從C++後端調用該函數... =/ – 2014-08-28 10:54:44
我發佈的只是最快的解決方案 - 但它是真正的很容易將外面的所有代碼帶入全球範圍,我會告訴你如何編輯我的答案。 – 2014-09-08 15:25:06
Thank @ webdev-dan – 2014-09-08 18:26:14