javascript - 如何仅将门票添加到特定卡目前所有门票都添加到第一张卡

标签 javascript

创建类似 trello 的东西来练习,所以到目前为止我已经创建了三张卡,在每张卡中我想创建门票,但我只能在我的第一张卡中做到这一点,我不知道如何在所有卡片分开,这是我的所有代码

我的html文件

//display username on home screen
let name = Cookies.get('UName');
let dis = name.split('@');
let dispname = dis[0];
document.getElementById('Uname').innerHTML = dispname;


//editable li items on click
document.addEventListener("click", function (e) {
    var elementsIdSelector = "editableLi";
    e = (e || window.event);
    e.preventDefault();
    var path = e.path;
    for (var i = 0; i < path.length - 4; i++) {
        if (path[i].tagName == "LI" && path[i].id == elementsIdSelector) {
            //Found a Li element with the id required ( Even dynamically created li would fires ! )

            path[i].addEventListener("click", function () {
                if (this.getAttribute("contenteditable") != "true") {
                    var wantEdit = window.confirm("You want to edit this card content ?");
                    if (wantEdit) {
                        this.setAttribute("contenteditable", true);
                        window.isEditingALi = this;
                        this.focus();
                    } else {
                        window.isEditingALi = false;
                    }
                }
            });

        }
    }
});

document.addEventListener("click", function (e) {
    e = (e || window.event);
    e.preventDefault();
    var path = e.path;
    var canGetReset = true;
    for (var i = 0; i < path.length - 4; i++) {
        if (path[i] == window.isEditingALi) canGetReset = false;
    }
    if (canGetReset && window.isEditingALi) {
        window.isEditingALi.removeAttribute("contenteditable");
        window.isEditingALi = false;

    }
});
//This is function to the dynamically created element would work
function createEditableLi() {
    const ul = document.querySelector("ul");
    const newLi = document.createElement("li");
    newLi.setAttribute("id", "editableLi");
    ul.appendChild(newLi);
}


//card dragable
$(function () {
    $("#parent, #parent1").sortable({
        connectWith: ".list-items"
    }).disableSelection();
});
body {
    font-family: Arial, sans-serif;
    display: flex;
    flex-direction: column;
}

.btn {
    display: flex;
    justify-content: center;
    align-items: center;
    font: inherit;
    background: none;
    border: none;
    color: inherit;
    padding: 0;
    cursor: pointer;
}

:focus {
    outline-color: #fa0;
}

:root {
    font-size: 11px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Masthead */

.masthead {
    flex-basis: 4rem;
    display: flex;
    align-items: center;
    padding: 0 0.8rem;
    margin: -8px -8px 0 -8px;
    background-color: #0067a3;
    box-shadow: 0 0.1rem 0.1rem rgba(0, 0, 0, 0.1);
}

.masthead .btn {
    background-color: #4c94be;
    border-radius: 0.3rem;
    transition: background-color 150ms;
}

.masthead .btn:hover {
    background-color: #3385b5;
}

.boards-menu {
    display: flex;
    flex-shrink: 0;
}

.boards-btn {
    flex-basis: 9rem;
    font-size: 1.4rem;
    font-weight: 700;
    color: #fff;
    margin-right: 0.8rem;
    padding: 0.6rem 0.8rem;
}

.boards-btn-icon {
    font-size: 1.7rem;
    padding-right: 1.2rem;
}

.board-search {
    flex-basis: 18rem;
    position: relative;
}

.board-search-input {
    height: 3rem;
    border: none;
    border-radius: 0.3rem;
    background-color: #4c94be;
    width: 100%;
    padding: 0 3rem 0 1rem;
    color: #fff;
}

.board-search-input:hover {
    background-color: #66a4c8;
}

.search-icon {
    font-size: 1.5rem;
    position: absolute;
    top: 50%;
    right: 0.8rem;
    transform: translateY(-50%) rotate(90deg);
    color: #fff;
}

.logo {
    flex: 1;
    font-family: "Courgette", cursive;
    font-size: 2.2rem;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.5);
    margin: 0 2rem;
    transition: color 150ms;
    text-align: center;
    white-space: nowrap;
    cursor: pointer;
}

.logo:hover {
    color: rgba(255, 255, 255, 0.8);
}

.logo-icon {
    padding-right: 0.4rem;
}

.user-settings {
    display: flex;
    height: 3rem;
    color: #fff;
}

.user-settings-btn {
    font-size: 1.5rem;
    width: 3.5rem;
    margin-right: 1.5rem;
}


/* Board info bar */

.board-info-bar {
    flex-basis: 3rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0.8rem 0;
    padding: 0 1rem;
    color: #f6f6f6;
}

sboard-info-bar .btn {
    font-size: 1.4rem;
    font-weight: 400;
    transition: background-color 150ms;
    padding: 0 0.6rem;
    border-radius: 0.3rem;
    height: 3rem;
}

.board-info-bar .btn:hover {
    background-color: #006aa8;
}

.private-btn-icon,
.menu-btn-icon {
    padding-right: 0.6rem;
    white-space: nowrap;
}

.board-title h2 {
    font-size: 1.8rem;
    font-weight: 700;
    white-space: nowrap;
}

/* Lists */

.lists-container::-webkit-scrollbar {
    height: 2.4rem;
}

.lists-container::-webkit-scrollbar-thumb {
    background-color: #66a3c7;
    border: 0.8rem solid #0079bf;
    border-top-width: 0;
}

.lists-container {
    display: flex;
    align-items: start;
    padding: 0 0.8rem 0.8rem;
    overflow-x: auto;
    height: calc(100vh - 8.6rem);
    margin-top: 60px;
}

.list {
    flex: 0 0 27rem;
    display: flex;
    flex-direction: column;
    background-color: #e2e4e6;
    max-height: calc(100vh - 11.8rem);
    border-radius: 0.3rem;
    margin-right: 1rem;
}

.list:last-of-type {
    margin-right: 0;
}

.list-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: #333;
    padding: 1rem;
}

.list-items {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-content: start;
    padding: 0 0.6rem 0.5rem;
    overflow-y: auto;
}

.list-items::-webkit-scrollbar {
    width: 1.6rem;
}

.list-items::-webkit-scrollbar-thumb {
    background-color: #c4c9cc;
    border-right: 0.6rem solid #e2e4e6;
}

.list-items li {
    font-size: 1.4rem;
    font-weight: 400;
    line-height: 1.3;
    background-color: #fff;
    padding: 0.65rem 0.6rem;
    color: #4d4d4d;
    border-bottom: 0.1rem solid #ccc;
    border-radius: 0.3rem;
    margin-bottom: 0.6rem;
    word-wrap: break-word;
    cursor: pointer;
}

.list-items li:last-of-type {
    margin-bottom: 0;
}

.list-items li:hover {
    background-color: #eee;
}

.add-card-btn {
    display: block;
    font-size: 1.4rem;
    font-weight: 400;
    color: #838c91;
    padding: 1rem;
    text-align: left;
    cursor: pointer;
}

.add-card-btn:hover {
    background-color: #cdd2d4;
    color: #4d4d4d;
    text-decoration: underline;
}


.add-list-btn {
    flex: 0 0 27rem;
    display: block;
    font-size: 1.4rem;
    font-weight: 400;
    background-color: #006aa7;
    color: #a5cae0;
    padding: 1rem;
    border-radius: 0.3rem;
    cursor: pointer;
    transition: background-color 150ms;
    text-align: left;
    margin-left: 15px;
}

.add-list-btn:hover {
    background-color: #005485;
}

.add-card-btn::after,
.add-list-btn::after {
    content: '...';
}

.input-style {
    width: 100%;
    height: 100%;
    border: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Trello</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="style.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
    <script src="/Authentication/auth.js"></script>
</head>

<body>
    <!-- Masthead -->
    <header class="masthead">
        <div class="boards-menu">
            <div class="board-search">
                <input type="search" class="board-search-input" aria-label="Board Search">
                <i class="fas fa-search search-icon" aria-hidden="true"></i>
            </div>
        </div>
        <div class="logo">
            <h1><i class="fab fa-trello logo-icon" aria-hidden="true"></i>Trello</h1>
        </div>
        <div class="setting-class">
            <a onclick="profile()">
                <h2 style="color: aliceblue;margin-right: 20px;" id="Uname"> </h2>
            </a>
        </div>
        <button onclick="logout()">Logout</button>
    </header>
    <!-- Lists container -->

    <section class="lists-container">
        <div class="list" id="dragabble_card">
            <h3 class="list-title">Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent">
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi()">Add a card</button>
        </div>
        <div class="list" id="dragabble_card">
            <h3 class="list-title">Pending Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent">
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi()">Add a card</button>
        </div>
        <div class="list" id="dragabble_card">
            <h3 class="list-title">Completed Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent">
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi()">Add a card</button>
        </div>
    </section>

    <script>
        function logout() {
            window.location.href = "/Authentication/login.html"
            document.cookie = "Auth=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        }
        function profile() {
            window.location.href = "/profile.html"
        }
    </script>
    <script src="script.js"></script>

</body>

</html>

我的js文件

//display username on home screen
let name = Cookies.get('UName');
let dis = name.split('@');
let dispname = dis[0];
document.getElementById('Uname').innerHTML = dispname;


//editable li items on click
document.addEventListener("click", function (e) {
    var elementsIdSelector = "editableLi";
    e = (e || window.event);
    e.preventDefault();
    var path = e.path;
    for (var i = 0; i < path.length - 4; i++) {
        if (path[i].tagName == "LI" && path[i].id == elementsIdSelector) {
            //Found a Li element with the id required ( Even dynamically created li would fires ! )

            path[i].addEventListener("click", function () {
                if (this.getAttribute("contenteditable") != "true") {
                    var wantEdit = window.confirm("You want to edit this card content ?");
                    if (wantEdit) {
                        this.setAttribute("contenteditable", true);
                        window.isEditingALi = this;
                        this.focus();
                    } else {
                        window.isEditingALi = false;
                    }
                }
            });

        }
    }
});

document.addEventListener("click", function (e) {
    e = (e || window.event);
    e.preventDefault();
    var path = e.path;
    var canGetReset = true;
    for (var i = 0; i < path.length - 4; i++) {
        if (path[i] == window.isEditingALi) canGetReset = false;
    }
    if (canGetReset && window.isEditingALi) {
        window.isEditingALi.removeAttribute("contenteditable");
        window.isEditingALi = false;

    }
});
//This is function to the dynamically created element would work
function createEditableLi() {
    const ul = document.querySelector("ul");
    const newLi = document.createElement("li");
    newLi.setAttribute("id", "editableLi");
    ul.appendChild(newLi);
}


//card dragable
$(function () {
    $("#parent, #parent1").sortable({
        connectWith: ".list-items"
    }).disableSelection();
});

我的CSS文件

body {
    font-family: Arial, sans-serif;
    display: flex;
    flex-direction: column;
}

.btn {
    display: flex;
    justify-content: center;
    align-items: center;
    font: inherit;
    background: none;
    border: none;
    color: inherit;
    padding: 0;
    cursor: pointer;
}

:focus {
    outline-color: #fa0;
}

:root {
    font-size: 11px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Masthead */

.masthead {
    flex-basis: 4rem;
    display: flex;
    align-items: center;
    padding: 0 0.8rem;
    margin: -8px -8px 0 -8px;
    background-color: #0067a3;
    box-shadow: 0 0.1rem 0.1rem rgba(0, 0, 0, 0.1);
}

.masthead .btn {
    background-color: #4c94be;
    border-radius: 0.3rem;
    transition: background-color 150ms;
}

.masthead .btn:hover {
    background-color: #3385b5;
}

.boards-menu {
    display: flex;
    flex-shrink: 0;
}

.boards-btn {
    flex-basis: 9rem;
    font-size: 1.4rem;
    font-weight: 700;
    color: #fff;
    margin-right: 0.8rem;
    padding: 0.6rem 0.8rem;
}

.boards-btn-icon {
    font-size: 1.7rem;
    padding-right: 1.2rem;
}

.board-search {
    flex-basis: 18rem;
    position: relative;
}

.board-search-input {
    height: 3rem;
    border: none;
    border-radius: 0.3rem;
    background-color: #4c94be;
    width: 100%;
    padding: 0 3rem 0 1rem;
    color: #fff;
}

.board-search-input:hover {
    background-color: #66a4c8;
}

.search-icon {
    font-size: 1.5rem;
    position: absolute;
    top: 50%;
    right: 0.8rem;
    transform: translateY(-50%) rotate(90deg);
    color: #fff;
}

.logo {
    flex: 1;
    font-family: "Courgette", cursive;
    font-size: 2.2rem;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.5);
    margin: 0 2rem;
    transition: color 150ms;
    text-align: center;
    white-space: nowrap;
    cursor: pointer;
}

.logo:hover {
    color: rgba(255, 255, 255, 0.8);
}

.logo-icon {
    padding-right: 0.4rem;
}

.user-settings {
    display: flex;
    height: 3rem;
    color: #fff;
}

.user-settings-btn {
    font-size: 1.5rem;
    width: 3.5rem;
    margin-right: 1.5rem;
}


/* Board info bar */

.board-info-bar {
    flex-basis: 3rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0.8rem 0;
    padding: 0 1rem;
    color: #f6f6f6;
}

sboard-info-bar .btn {
    font-size: 1.4rem;
    font-weight: 400;
    transition: background-color 150ms;
    padding: 0 0.6rem;
    border-radius: 0.3rem;
    height: 3rem;
}

.board-info-bar .btn:hover {
    background-color: #006aa8;
}

.private-btn-icon,
.menu-btn-icon {
    padding-right: 0.6rem;
    white-space: nowrap;
}

.board-title h2 {
    font-size: 1.8rem;
    font-weight: 700;
    white-space: nowrap;
}

/* Lists */

.lists-container::-webkit-scrollbar {
    height: 2.4rem;
}

.lists-container::-webkit-scrollbar-thumb {
    background-color: #66a3c7;
    border: 0.8rem solid #0079bf;
    border-top-width: 0;
}

.lists-container {
    display: flex;
    align-items: start;
    padding: 0 0.8rem 0.8rem;
    overflow-x: auto;
    height: calc(100vh - 8.6rem);
    margin-top: 60px;
}

.list {
    flex: 0 0 27rem;
    display: flex;
    flex-direction: column;
    background-color: #e2e4e6;
    max-height: calc(100vh - 11.8rem);
    border-radius: 0.3rem;
    margin-right: 1rem;
}

.list:last-of-type {
    margin-right: 0;
}

.list-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: #333;
    padding: 1rem;
}

.list-items {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-content: start;
    padding: 0 0.6rem 0.5rem;
    overflow-y: auto;
}

.list-items::-webkit-scrollbar {
    width: 1.6rem;
}

.list-items::-webkit-scrollbar-thumb {
    background-color: #c4c9cc;
    border-right: 0.6rem solid #e2e4e6;
}

.list-items li {
    font-size: 1.4rem;
    font-weight: 400;
    line-height: 1.3;
    background-color: #fff;
    padding: 0.65rem 0.6rem;
    color: #4d4d4d;
    border-bottom: 0.1rem solid #ccc;
    border-radius: 0.3rem;
    margin-bottom: 0.6rem;
    word-wrap: break-word;
    cursor: pointer;
}

.list-items li:last-of-type {
    margin-bottom: 0;
}

.list-items li:hover {
    background-color: #eee;
}

.add-card-btn {
    display: block;
    font-size: 1.4rem;
    font-weight: 400;
    color: #838c91;
    padding: 1rem;
    text-align: left;
    cursor: pointer;
}

.add-card-btn:hover {
    background-color: #cdd2d4;
    color: #4d4d4d;
    text-decoration: underline;
}


.add-list-btn {
    flex: 0 0 27rem;
    display: block;
    font-size: 1.4rem;
    font-weight: 400;
    background-color: #006aa7;
    color: #a5cae0;
    padding: 1rem;
    border-radius: 0.3rem;
    cursor: pointer;
    transition: background-color 150ms;
    text-align: left;
    margin-left: 15px;
}

.add-list-btn:hover {
    background-color: #005485;
}

.add-card-btn::after,
.add-list-btn::after {
    content: '...';
}

.input-style {
    width: 100%;
    height: 100%;
    border: none;
}

现在我想要的是用户可以在每张卡中创建门票 车道,所以任何帮助将不胜感激,我对 js 很陌生

最佳答案

我认为如果你只有树 ul 你可以将 ul id 传递给你的函数

function createEditableLi(ulId) { //here is change to your code
    const ul = document.getElementById(ulId);  ////here is change to your code
    const newLi = document.createElement("li");
    newLi.setAttribute("id", "editableLi");
    ul.appendChild(newLi);
}

这里是对 html 的更改

<section class="lists-container">
        <div class="list" id="dragabble_card">// here change
            <h3 class="list-title">Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent_1">
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi('parent_1')">Add a card</button> //here is change to you code
        </div>
        <div class="list" id="dragabble_card">// here change
            <h3 class="list-title">Pending Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent_2">//here change
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi('parent_2')">Add a card</button> // here change
        </div>
        <div class="list" id="dragabble_card">
            <h3 class="list-title">Completed Tasks</h3>
            <form action="/public/card-controller.js" method="POST">
                <ul class="list-items" id="parent_3">//here change
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                    <li id="editableLi"> </li>
                </ul>
            </form>
            <button class="add-card-btn btn" onclick="createEditableLi('parent_3')">Add a card</button> // here change
        </div>
    </section>

关于javascript - 如何仅将门票添加到特定卡目前所有门票都添加到第一张卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57938790/

相关文章:

javascript - KnockoutJS 和递归模板

javascript - 如何在单个表单上使用 plupload 的多个实例

javascript - 使用 Javascript 清空 FileUpload 文本

javascript - 修复显示中的背景线性颜色 : flex overflow scroll content - CSS

javascript - 如果 AJAX 成功/失败后,jquery 无法在内部工作

javascript - 无法在 ng-repeat 中设置 AngularJS 变量

javascript - EventSource 重新连接时丢失了一些 Server Sent 事件

javascript - 为什么我不能拆分一个字符串然后调用每个字符串?

javascript - 在 UIWebview 中启用水平滚动

Javascript 函数indexOf 返回不正确的结果