简体   繁体   English

您如何定位此父级,从一个孩子开始,使用仅带有纯JavaScript的类,仅使另一个子div显示?

[英]How do you target THIS Parent, from a child to make another child div show using nothing but classes with pure javascript?

So I have a bunch of data thats being loaded in dynamically so I have to use classes to pull this off. 因此,我有一堆动态加载的数据,因此我必须使用类来实现。

Just using pure javascript and not jquery, how do I make it so that when you click view more in a div, it makes the hidden div only show from THIS parent? 仅使用纯JavaScript而不使用jquery,如何做到这一点,以便当您单击div中的view more时,隐藏的div仅从父级显示?

在此处输入图片说明

Structurally, you have a couple choices. 从结构上讲,您有两种选择。

When .classViewMore is clicked, you can get the target of the event (most likely in the this pointer) that will be the .classViewMore item that was clicked on. 单击.classViewMore ,可以获取事件的目标(很可能在this指针中),该事件的目标将是单击的.classViewMore项。

From that object, you can up to the parent (that gets you the Child item in your common parent. 从该对象开始,您可以升至父对象(这将使您的父项成为Child项)。

From the Child , you can either go to a sibling to find Hidden Child or you can go up to the .classDiv parent and then find the containing .classHiddenDiv . Child那里,您可以转到同级兄弟中找到“ Hidden Child ,也可以转到.classDiv父对象,然后找到包含的.classHiddenDiv

It's all about using the DOM hierarchy to find the other item that shares a common parent. 这就是使用DOM层次结构查找共享同一父项的其他项。 Without actual HTML, it's a little hard to give exact code, but it would be something like this: 如果没有实际的HTML,则很难提供确切的代码,但这将是这样的:

function classViewMoreClickHandler(e) {
    // find next sibling of our parent
    var childElem = this.parentNode;
    var hiddenChildElem = childElem.nextSibling; 
}

or, going up to the .classDiv parent, it would look like this: 或者,转到.classDiv父级,它看起来像这样:

function classViewMoreClickHandler(e) {

    var parent = this.parentNode.parentNode;
    var hiddenChild = parent.querySelectorAll(".classHiddenDiv")[0];
}

If you're going to work in plain JS regularly, then you probably want an analog for jQuery's .closest() method in plain javascript which finds an ancestor that matches a specific selector which can be really, really useful for this up to some common parent kind of DOM hiearchy navigation. 如果您要定期使用纯JS工作,那么您可能希望使用纯javascript的jQuery的.closest()方法的类似物,该方法可以找到与特定选择器匹配的祖先,这对于某些常见的选择器可能是非常有用的父类的DOM导航。 For example, you could make a bit more robust solution using this: 例如,您可以使用以下方法提供更强大的解决方案:

function hasClass(elem, cls) {
    var str = " " + elem.className + " ";
    var testCls = " " + cls + " ";
    return(str.indexOf(testCls) != -1) ;
}

function closest(start, cls) {
    while (start && start !== document.documentElement && !hasClass(start, cls)) {
        start = start.parentNode;
    }
    return start;
}

function classViewMoreClickHandler(e) {

    var p = closest(this, ".classDiv");
    if (p) {
        var hiddenChild = p.querySelectorAll(".classHiddenDiv")[0];
        // do whatever you want to hiddenChild here
    }
}

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

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