简体   繁体   English

Select 特定兄弟元素

[英]Select specific sibling element

In my app I want to get sibling elements of the task-update element, when it is clicked, and then change their classes.在我的应用程序中,我想获取任务更新元素的同级元素,当它被单击时,然后更改它们的类。

HTML: HTML:

<template id="template-task">
    <li>
        <span id="task-title"></span>
        <input id="task-title-input" class="hide">
        <span id="task-text"></span>
        <input id="task-text-input" class="hide">
        <span id="task-update">Update</span>
        <span id="task-save" class="hide">Save</span>
        <span id="task-del">Delete</span>

    </li>
</template>

JS:记者:

this.closest("#task-text").classList.add("hide");
this.closest("#task-text-input").classList.remove("hide");
this.closest("#task-title").classList.add("hide");
this.closest("#task-title-input").classList.remove("hide");
this.closest("#task-save").classList.remove("hide");

This solution sadly doesn't work for me.遗憾的是,这个解决方案对我不起作用。 From what I read it's because closest looks only for parent elements.根据我的阅读,这是因为closest只查找父元素。 Is there another way to do it?还有另一种方法吗? Selecting these items with nextSibling , prevSibling doesn't look well to me.使用nextSiblingprevSibling选择这些项目对我来说不太好。

If you get the parent node you then can access its children.如果获得父节点,则可以访问其子节点。

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

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