单击+按钮时,我必须添加新元素,并且该元素应从该div转到无序列表。每次单击+按钮时,元素都会移至我创建的最后一个div。我知道我的错误是ul中的javascript是uls数组,但不知道如何解决我认为我必须使用this关键字,但是却不知道如何使用。有经验的人可以给我一些建议吗?



let check = (function(){

    function isActive (elem) {
        if(elem.checked == true){
            elem.parentNode.style.backgroundColor = 'forestGreen';
        }else{
            elem.parentNode.style.backgroundColor = 'white';
        }
    }

    return {
        isActive: isActive
    }

})();


let section = (function(){

    function Section(){
        //create section div
        let newDiv = document.createElement('div');
        newDiv.setAttribute('class', 'wrapper');
        let wrapper = document.getElementsByClassName('wrapper');
        let len = wrapper.length-1;
        wrapper[len].parentNode.appendChild(newDiv);

        //create div for list items
        let innerDiv = document.createElement('div');
        innerDiv.setAttribute('id', 'business');
        newDiv.appendChild(innerDiv);

        //create title
        let title = document.createElement('h3');
        let txtTitle = document.getElementById('title').value;
        let createTitle = document.createTextNode(txtTitle);
        title.appendChild(createTitle);
        title.setAttribute('tag','h3');
        innerDiv.appendChild(title);

        //create new Unordered List
        let newUl = document.createElement('ul');
        newUl.setAttribute('tag', 'ul');
        innerDiv.appendChild(newUl);

        //create add button
        let btn = document.createElement('input');
        btn.setAttribute('type', 'button');
        btn.setAttribute('value', '+');
        btn.setAttribute('class', 'rightInputs');
        newDiv.appendChild(btn);
        btn.setAttribute('onclick', 'section.createElement()');

        //create field for the elements
        let txtField = document.createElement('input');
        txtField.setAttribute('value', 'Add item...');
        txtField.setAttribute('type', 'text');
        txtField.setAttribute('class', 'rightInputs');
        txtField.setAttribute('onMouseOver', 'clearContent(this)');
        newDiv.appendChild(txtField);
    }

    function createElement(){
        //create LI
        let elem = document.createElement('li');
        let inputs = document.getElementsByClassName('rightInputs');
        let lastInp = inputs[inputs.length-1].value;
        let txtLi = document.createTextNode(lastInp);
        let ul = document.getElementsByTagName('ul');
        //create checkbox
        let chBox = document.createElement('input');
        chBox.setAttribute('type', 'checkbox');
        chBox.setAttribute('onclick', 'check.isActive(this)');
        //------------
        elem.setAttribute('tag', 'li');
        elem.appendChild(chBox);
        elem.appendChild(txtLi);
        ul[ul.length-1].appendChild(elem);
    }

    function addToDOM(){
        return new Section();
    }

    return {
        addToDOM: addToDOM,
        createElement: createElement
    }

})();

function clearContent(elem){
    if(elem.value == 'Add item...' || elem.value == 'Title..'){
        elem.value = '';
    }
}

#mainDiv {
    width: 500px;
    height: 600px;
    border: 2px solid black;
    margin: auto;
}

h1{
    text-align: center;
}

#title{
    width: 150px;
    color: black;
    margin-left: 20px;
}

#addSection {
    margin-left: 10px;
}

#container {
    overflow: auto;
    width: 450px;
    height: 450px;
    border: 5px double gray;
    margin-bottom: 20px;
    margin-left: 20px;
}

#shoppingList {
    overflow: auto;
    width: 375px;
    height: 165px;
    border: 1px solid black;
    margin: auto;
    margin-top: 20px;
}

#business {
    overflow: auto;
    width: 375px;
    height: 165px;
    border: 1px solid black;
    margin: auto;
    margin-top: 20px;
}

ul {
    margin-top: 5px;
    padding-right: 10px;
    padding-left: 10px;
    list-style: none;
}

li {
    border-bottom: 1px solid black;
    width: 100%;
}

input {
    margin-top: 10px;
    margin-bottom: 10px;
}

.rightInputs {
    float: right;
    margin-right: 20px;
}

h3 {
    text-align: right;
    margin: 10px 10px 0px 0px;
}

.wrapper {
    width: 400px;
    height: 250px;
    margin: auto;
}

<div id='mainDiv'>
        <h1> Tuesday <em>TODO</em> List </h1>
        <div id='container'>
                <div class='wrapper'>
                    <div id='shoppingList'>
                        <h3> Shopping List </h3>
                        <ul id='forShopping'>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Air-freshener</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Pampers</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Newspapper</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Toilet paper</input></li>
                        </ul>
                    </div>
                    <input class='rightInputs' type='button' value='+' onclick='section.createElement()'/>
                    <input class='rightInputs' type='text' value='Add item...'
                            onMouseOver='clearContent(this)'/>
                </div>
                <div class='wrapper'>
                    <div id='business'>
                        <h3> Business List </h3>
                        <ul id='forBusiness'>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Inspect fiscal year report</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Lunch with board of directors</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Fire Jackson</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Take a nap</input></li>
                            <li><input type='checkbox' onclick='check.isActive(this)'> Arrange meeting with investors</input></li>
                        </ul>
                    </div>
                    <input class='rightInputs' type='button' value='+' onclick='section.createElement()'/>
                    <input class='rightInputs' type='text' value='Add item...'
                            onMouseOver='clearContent(this)'/>
                </div>
        </div>
        <input id='title' type='text' value='Title..' onMouseOver='clearContent(this)'/>
        <input id='addSection' type='button' value='New Section' onclick='section.addToDOM()'/>
    </div>





HTML code

JavaScript code

CSS code

最佳答案

像这样更改您的+按钮的html代码

<input class='rightInputs' type='button' value='+' data-index="0" onclick='section.createElement(event)'/>


使用data-index,将设置要更改的列表的索引,然后将event对象传递给函数createElement。比这样更新您的createElement函数:

function createElement(e) {
  //..
  ul[e.target.dataset.index].appendChild(elem);
}


您将使用按钮中的属性data-index定位右列表。

还更新您的Section函数,该函数创建新的部分,并将data-index属性设置为新按钮:

btn.setAttribute('data-index', document.getElementsByTagName('ul').length - 1);


并更新click属性:

btn.setAttribute('onclick', 'section.createElement(event)');


这是工作提琴https://jsfiddle.net/dm06a7db/

关于javascript - 到达正确的UL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40765494/

10-12 01:08
查看更多