为什么js最接近的方法找不到同级元素? [英] why js closest method not find sibling element?

查看:54
本文介绍了为什么js最接近的方法找不到同级元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:据我所知, closest()方法在DOM树中搜索与指定CSS选择器匹配的最近元素.

Note: as far as i know that closest() method searches up the DOM tree for the closest element which matches a specified CSS selector.

当我单击两个 li 元素之间的 margin 空格时..它返回null ...但是如果我用 padding-bottom 替换 margin-bottom:15px; ...一切都很好...

When i click on margin space between two li element .. it's return null... but if i replace margin-bottom: 15px; with padding-bottom... everything is okay...

CSS代码

.amenities-filters-inner ul li{
    margin-bottom: 15px;
}

图片,如果我单击红色标记区域..,则找不到同级 li 元素..

Image if i click on red mark area.. it's not found sibling li element ..

示例代码

   document.querySelectorAll(".amenities-filters-inner ").forEach(function(item){
    item.addEventListener("click",function(e){
        e.preventDefault();

        let element = null;
        element = e.target.closest(".amenity_id");
        // element = e.target.querySelector(".amenity_id");
         element = element.children[0].children[1];
         element.checked == true ? element.checked = false :  element.checked =true;
       
        let dataId =  + element.getAttribute('data-id');
        console.log(element)
        console.log(dataId)
       
    })
})

 .amenities-filters-inner{
        
            border: 2px dashed royalblue;
            max-width: 300px;
            padding: 1rem;
        }
        ul{
            list-style: none;
            width: 100%;
            margin: 0;
            padding: 0;
           
        }
        li{
            border: 2px solid rgb(133, 129, 129);
            padding: 3px;
        }


.amenities-filters-inner:last-child{
    margin-bottom: 0;
}

.amenities-filters-inner ul li{
    margin-bottom: 20px;
}
.amenities-filters-inner ul li:last-child{
    margin-bottom: 0;
}

.check {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
    padding-left: 31px;
    margin-bottom: 0;
    padding-right: 0;
    cursor: pointer;
    font-size: 14px; 
    color: #646464;
}
.check  strong{
    color: #969696;
    font-size: 14px; 
    font-weight: 400;
}

.check input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}


.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 17px;
    width: 17px;
    background-color: #fff ;
    border-color: #646464;
    border-style:solid;
    border-width:1px;
    border-radius: 2px;
}

.check input:checked ~ .checkmark {
    background-color: #fff;
    border-color: #007FEB;
}
.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}
.check input:checked ~ .checkmark:after {
    display: block;
}
.check .checkmark:after {
    left: 5px;
    top: 1px;
    width: 5px;
    height: 10px;
    border: solid ;
    border-color:#007FEB;
    border-width: 0 2px 2px 0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
}

   <div class="amenities-filters-inner">
            <ul>
                <li class="amenity_id">
                    <label class="check ">One <strong>One</strong>
                        <input type="checkbox" class="amenity_data" data-id="1" name="is_name">
                        <span class="checkmark"></span>
                    </label>
                </li>
                <li class="amenity_id">
                    <label class="check ">Two <strong>Two</strong>
                        <input type="checkbox" class="amenity_data" data-id="2"name="is_name">
                        <span class="checkmark"></span>
                    </label>
                </li>
<li class="amenity_id">
                    <label class="check ">Three<strong>Three</strong>
                        <input type="checkbox" class="amenity_data" data-id="3"name="is_name">
                        <span class="checkmark"></span>
                    </label>
                </li>
              
               
            </ul>
        </div>

推荐答案

.closest方法仅遍历父级,而不遍历子级.

.closest method only traverses parents, not children.

该区域位于.amenities-filters-inner容器中,而不位于list元素内,因此不会找到.amenities-id

That area is in container .amenities-filters-inner not inside list element, so it won't find the .amenities-id

更改容器的背景色以进行调试.

Change the background color of container to debug.

.amenities-filters-inner{
  background: red;
}

如果您不希望列表项之间出现间隙,请不要使用边距.如果要制作

If you don't want gaps between list items, don't use margin. Use padding-top and padding-bottom instead, if you want to make the

这篇关于为什么js最接近的方法找不到同级元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆