我的jQuery脚本是一个非常大的文件,但我已将其修改为此问题的最相关部分,如下所示;
$(document).ready(function() {
"use strict";
$(document).on('click', function(e) {
if (($(e.target).attr('data-action')) || $(e.target).parent().data('action')) {
if ($(e.target).attr('data-action')) {
action = $(e.target).data('action');
} else {
action = $(e.target).parent().data('action');
}
switch (action) {
case 'mail-pin':
// Code for 'mail-pin' click
break;
default:
return;
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
<!-- === INBOX LIST STARTS === -->
<div class="inbox-content clearfix">
<div class="Head">
<!-- Code for inbox header -->
</div>
<div class="Body clearfix">
<div class="Pinned">
<div class="Mail clearfix" data-mail-ID="1234">
<!-- Mail Item -->
</div>
<div class="Mail clearfix" data-mail-ID="1235">
<!-- Mail Item -->
</div>
</div>
<div class="Standard">
<div class="Mail clearfix" data-mail-ID="1233">
<!-- Mail Item -->
</div>
<div class="Mail clearfix" data-mail-ID="1236">
<!-- Mail Item -->
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
首先,我知道如何通过下面看到的方式检查物品从.Pinned元素或.Standard其他元素移动到何处;
case 'mail-pin':
console.log('Hello');
if ($(e.target).closest('.Mail').parent().hasClass('Pinned')) {
console.log('It is pinned.');
} else {
console.log('It is not pinned.');
}
break;
Run Code Online (Sandbox Code Playgroud)
我不明白如何实现的是如何将内容附加到正确的位置,并由此我参考从中获取的顺序,data-mail-ID="1233"以便在单击以固定或取消固定时,内容将附加到该位置,因此如果您单击以固定邮件ID X,它将在X - 1后追加,反之亦然,如果该项目取消固定.
每个列表每页只显示20个项目,并且在单击转到下一页或上一页时,该页面将获取在单击以固定或取消固定时已修改的内容,因此该脚本仅与可以找到的ID相关在该页面上,因此意味着如果您取消固定ID 123和122无法找到,它只是被移除并且为了固定,只有在.Pinned可见的情况下才会出现附加部分,否则该项目只是删除.
搜索具有较大 id 的第一封邮件,并将单击的元素附加在其前面。
(下面的函数使用闭包来存储相关的 DOM 部分,因此我只需要查询 DOM 一次。这并不是真正需要的,但这就是我做这些事情的方式^^)
var togglePinState = (function () {
var pinned = document.querySelector(".Pinned"),
unpinned = document.querySelector(".Standard"),
pinnedMails = pinned.getElementsByClassName("Mail"),
unpinnedMails = unpinned.getElementsByClassName("Mail");
// .getElementsByClassName() because it returns a live HTMLCollection
// pinnedMails and unpinnedMails will always have the currently un/pinned "mails" without re-querying the DOM
return function (mailItem) {
var mailId = parseInt(mailItem.getAttribute("data-mail-ID"), 10),
mailItemIsPinned = (mailItem.parentNode.className === "Pinned"),
newParent = (mailItemIsPinned ? unpinned : pinned),
mailsToCheck = (mailItemIsPinned ? unpinnedMails : pinnedMails),
// variables for the loop below
i = 0,
l = mailsToCheck.length,
currentId;
for (; i < l; i++) {
currentId = parseInt(mailsToCheck[i].getAttribute("data-mail-ID"), 10);
if (currentId > mailId) {
// insert before first pinned mail with a bigger id
mailsToCheck[i].insertAdjacentElement("beforebegin", mailItem);
return;
}
}
// at this point we have not found a mail with a bigger id so we can safely append it at the end of the list
newParent.appendChild(mailItem);
}
}());
Run Code Online (Sandbox Code Playgroud)
要在脚本中使用它,只需在mail-pin分支中调用它:
case 'mail-pin':
togglePinState(e.target);
break;
Run Code Online (Sandbox Code Playgroud)
这是正在运行的函数:
case 'mail-pin':
togglePinState(e.target);
break;
Run Code Online (Sandbox Code Playgroud)
var togglePinState = (function() {
var pinned = document.querySelector(".Pinned"),
unpinned = document.querySelector(".Standard"),
pinnedMails = pinned.getElementsByClassName("Mail"),
unpinnedMails = unpinned.getElementsByClassName("Mail");
// .getElementsByClassName() because it returns a live HTMLCollection
// pinnedMails and unpinnedMails will always have the currently un/pinned "mails" without re-querying the DOM
return function(mailItem) {
var mailId = parseInt(mailItem.getAttribute("data-mail-ID"), 10),
mailItemIsPinned = (mailItem.parentNode.className === "Pinned"),
newParent = (mailItemIsPinned ? unpinned : pinned),
mailsToCheck = (mailItemIsPinned ? unpinnedMails : pinnedMails),
// variables for the loop below
i = 0,
l = mailsToCheck.length,
currentId;
for (; i < l; i++) {
currentId = parseInt(mailsToCheck[i].getAttribute("data-mail-ID"), 10);
if (currentId > mailId) {
// insert before first pinned mail with a bigger id
mailsToCheck[i].insertAdjacentElement("beforebegin", mailItem);
return;
}
}
// at this point we have not found a mail with a bigger id so we can safely append it at the end of the list
newParent.appendChild(mailItem);
}
}());
document.querySelector("div.inbox-content")
.addEventListener("click", function(e) {
if (e.target.nodeName === "DIV" && e.target.classList.contains("Mail")) {
togglePinState(e.target);
}
});Run Code Online (Sandbox Code Playgroud)
div { padding: 2px }
div.Mail {
border: dotted 1px black;
text-align: center;
}
.Pinned { border: solid 1px red }
.Standard { border: solid 1px blue }Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |