我试图通过使用jQuery UI的<span>
函数来设置.position()
元素的位置,但我无法让它工作。我希望它们位于可排序<ul>
的列表元素的旁边。
这是我的HTML结构:
<div id="#tabs-settings-wrapper">
<p>You can order them by drag and drop.</p>
<ul id="tabs-sortable">
<li class="ui-state-default tabs-sortable-item" id="tab-0" data-position="0">overview</li>
<li class="ui-state-default tabs-sortable-item" id="tab-1" data-position="1">detail 1</li>
<li class="ui-state-default tabs-sortable-item" id="tab-2" data-position="2">detail 2</li>
</ul>
<button id="tabs-add-new-button">Add new</button>
<!-- These are created dynamically, see JavaScript below -->
<span id="tab-0-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="0"></span>
<span id="tab-1-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="1"></span>
<span id="tab-2-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="2"></span>
</div>
和我的JavaScript代码:
$(function () {
$("#tabs-sortable").sortable({
placeholder: "ui-state-highlight"
});
// add remove icons to every li
$(".tabs-sortable-item").each(function () {
var position = $(this).data("position");
var id = "#" + $(this).prop("id");
var removeIconID = '#tab-' + position + '-remove-icon';
$('<span id="tab-' + position + '-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="' + position + '"></span>').appendTo("#tabs-settings-wrapper");
$(removeIconID).position({
my: "right",
at: "left",
of: id,
collision: "none"
});
});
$("#tabs-add-new-button").button().click(function () {
// numbering seems strange, but is more human-like (and logical)
var maxPosition = $(".tabs-sortable-item").length - 1;
var newPosition = maxPosition + 1;
$('<li class="ui-state-default tabs-sortable-item" id="tab-' + newPosition + '">' +
'<input id="tabs-add-new-input" style="margin-top: -5px;" placeholder="Enter a name" />' +
'</li>').appendTo("#tabs-sortable");
$('<button class="tabs-add-new-save-button" id="tab-' + newPosition + '-save-button" data-position="' + newPosition + '">Save</button>').appendTo("#tabs-settings-wrapper");
$("#tab-" + String(newPosition) + "-save-button").button();
$("#tab-" + String(newPosition) + "-save-button").position({
my: "left",
at: "right+20px",
of: "#tab-" + String(newPosition) + ""
});
$('<button class="tabs-add-new-cancel-button" id="tab-' + newPosition + '-cancel-button" data-position="' + newPosition + '">Cancel</button>').appendTo("#tabs-settings-wrapper");
$("#tab-" + String(newPosition) + "-cancel-button").button();
$("#tab-" + String(newPosition) + "-cancel-button").position({
my: "left",
at: "right+10px",
of: "#tab-" + String(newPosition) + "-save-button"
});
var li = $("#tabs-sortable li");
var innerWidth = li.innerWidth();
var paddingLeft = li.css("padding-left").substring('p')[1];
var paddingRight = li.css("padding-right").substring('p')[1];
$(".tabs-sortable-item input").last().focus().css("width", (innerWidth - (paddingLeft + paddingRight)));
$("#tabs-sortable").sortable("refresh");
// prevent the user from adding multiple list items at once
$("#tabs-add-new-button").button("disable");
});
});
但是它们出现在添加新按钮(它们是用HTML代码编写的)之后,而不是我期望的那样:
修改
我点击添加新按钮时添加了两个按钮,定位它们似乎有效。我将此代码添加到JavaScript部分。这是它的外观:
答案 0 :(得分:1)
由于position
列表,我认为在这种情况下你应该避免使用ul
。
我希望在每个span
中添加li
,并在必要时通过代码更新/隐藏它。
<div id="#tabs-settings-wrapper">
<p>You can order them by drag and drop.</p>
<ul id="tabs-sortable">
<li class="ui-state-default tabs-sortable-item" id="tab-0" data-position="0">
overview
<span id="tab-0-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="0"></span>
</li>
<li class="ui-state-default tabs-sortable-item" id="tab-1" data-position="1">
detail 1
<span id="tab-1-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="1"></span>
</li>
<li class="ui-state-default tabs-sortable-item" id="tab-2" data-position="2">
detail 2
<span id="tab-2-remove-icon" class="tabs-remove-icon fa fa-trash fa-lg" data-position="2"></span>
</li>
</ul>
<button id="tabs-add-new-button">Add new</button>
</div>
或者直接在span
(例如,通过li
)添加带有代码的document.getElementById("tab-1").appendChild(span);
。