简体   繁体   English

如果列表项单击并更改其他项,则更改背景颜色

[英]Changing background color if a list-item onClick and reset other items

for (const doc of docs) {
    // create a `div` element
    const div = document.createElement("li");
    div.classList.add("list-group-item");
    div.style.border = "none";

    // add a text node to it
    div.appendChild(document.createTextNode(doc.name));

    // add event listeners to change its background
    div.addEventListener("mouseover", e => { div.style.background = "#e9ecef"; });
    if (div.style.backgroundColor !== "#e9ecef") {
        div.addEventListener("mouseout", e => { div.style.background = "white"; });
    }
    // add a `click` listener
    div.addEventListener("click", e => {
        updateInput(doc);

        var listGroup = document.getElementById("list-group-row").getElementsByTagName('li');
    });

    // add the new div to the container
    container.appendChild(div);
}

This is a loop that I add list-items into my container. 这是我将列表项添加到容器中的循环。 The addEventListener for mouseover and mouseout gives an effect for changing background color when we hover and change back to white when hovered out. mouseovermouseoutaddEventListener提供了一种效果,当我们将鼠标悬停时会更改背景颜色,而当鼠标悬停时会变回白色。

My problem is in the addEventListener click part. 我的问题是在addEventListener click部分。 I tried to change the .style.backgroundColor of div , but obviously this changes the color of other list-items to since it's inside a for-loop. 我试图更改div.style.backgroundColor ,但是显然,这将其他列表项的颜色更改为,因为它位于for循环内。

What is the best way to make individual list-item to change backgroundColor onClick and change back when other item is clicked? 什么是使单个列表项更改backgroundColor onClick并在单击其他项目时又变回的最佳方法?

I also want to keep the mouseover and mouseout effect. 我也想保持mouseovermouseout效果。

FULL CODE: 完整代码:

    VSS.getService(VSS.ServiceIds.ExtensionData)
    // the callback on the next line returns a promise, which the JavaScript engine will follow, so you don't need to nest the next `then`
        .then((dataService) => dataService.getDocuments('MyCollection2'))
        .then((docs) => {
            // keep a reference to the element instead of searching for it in each loop.
            const container = document.getElementById('items');

            // this loop will remove any existing children
            while (container.firstChild !== null) {
                container.removeChild(container.firstChild);
            }

            // `for...of` is a simpler way to iterate over a collection
            for (const doc of docs) {
                // create a `div` element
                const div = document.createElement("li");
                div.classList.add("list-group-item");
                div.style.border = "none";

                // add a text node to it
                div.appendChild(document.createTextNode(doc.name));

                // add event listeners to change its background
                div.addEventListener("mouseover", e => { div.style.background = "#e9ecef"; });
                div.addEventListener("mouseout", e => { div.style.background = "white"; });

                // add a `click` listener
                //get all the elements with calss list-group-item
                [...document.querySelectorAll('.list-group-item')].forEach(function(item) {
                    // iterate and add event lstener to each of them
                    item.addEventListener('click', function(elem) {
                        // check if any element have a class active
                        // if so then remove the class from it
                        let getElemWithClass = document.querySelector('.clicked');
                        if (getElemWithClass !== null) {
                            getElemWithClass.classList.remove('clicked');
                            getElemWithClass.classList.add('unClicked')
                        }
                        //add the active class to the element from which click event triggered
                        item.classList.add('clicked')

                    })
                })


                // add the new div to the container
                container.appendChild(div);
            }
        });

You may not need to attach the event inside the loop. 您可能不需要在循环内附加事件。 Create a new function to attach the event but call this function only when the loop have finished it's execution depends on the availability of the element with class list-group-item 创建一个新函数来附加事件,但仅在循环完成后才调用此函数,取决于类list-group-item的元素的可用性

 //get all the elements with calss list-group-item [...document.querySelectorAll('.list-group-item')].forEach(function(item) { // iterate and add event lstener to each of them item.addEventListener('click', function(elem) { // check if any element have a class active // if so then remove the class from it let getElemWithClass = document.querySelector('.active'); if (getElemWithClass !== null) { getElemWithClass.classList.remove('active'); } //add the active class to the element from which click event triggered item.classList.add('active') }) }) 
 .active { color: green; font-size: 24px; } 
 <li class="list-group-item"> 1 </li> <li class="list-group-item"> 2 </li> <li class="list-group-item"> 3 </li> <li class="list-group-item"> 4 </li> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM