查找具有某个类向上的最近div的Child元素

时间:2021-12-20 03:35:43

I am having elements as follows:

我的元素如下:

<div class="parent">
    <div class="something">
        <div class="something-inner" data-id="xyz">
            some contents
        </div>
    </div>
    <div class="next">
        <a onclick="doSomething();"></a>
    </div>
</div>

in function doSomething(), I need to get the data-id of div with "something-inner". I have tried using this code,

在函数doSomething()中,我需要使用“something-inner”获取div的data-id。我试过用这个代码,

$(this).parent(".parent").find(".something-inner").data("id");

But it does not select the ".something-inner" div and get the data "xyz".

但它没有选择“.something-inner”div并获取数据“xyz”。

4 个解决方案

#1


1  

Try This:

尝试这个:

Dom:

DOM:

<div class="parent">
<div class="something">
    <div class="something-inner" data-id="xyz">
        some contents
    </div>
</div>
<div class="next">
    <a onclick="doSomething(this);"></a>
</div>
</div>

Js code:

Js代码:

function doSomething(obj){
alert($(obj).closest('.parent').find(".something-inner").data("id"););
}

working fiddle

工作小提琴

#2


0  

    <div class="parent">
        <div class="something">
            <div class="something-inner" data-id="xyz">
                some contents
            </div>
        </div>
        <div class="next">
            <a onclick="doSomething(this);"></a>
        </div>
    </div>
<script>

    function doSomething(obj)
    {
          console.log($(obj).parent().prev().find(".something-inner").attr("data-id"));
    }

</script>

#3


-1  

Basically .parent() will select the element's immediate parent, You should use .closest() to achieve the result of your expectation.

基本上.parent()将选择元素的直接父级,您应该使用.closest()来实现期望的结果。

Try,

尝试,

HTML:

HTML:

<a onclick="doSomething(e);"></a>

JS:

JS:

$(e).closest(".parent").find(".something-inner").data("id");

#4


-1  

try something like this

尝试这样的事情

html

HTML

<a onclick="doSomething(this);"></a>

javascript

JavaScript的

function doSomething(obj){
    $(obj).parent(".parent").find(".something-inner").data("id");
}

#1


1  

Try This:

尝试这个:

Dom:

DOM:

<div class="parent">
<div class="something">
    <div class="something-inner" data-id="xyz">
        some contents
    </div>
</div>
<div class="next">
    <a onclick="doSomething(this);"></a>
</div>
</div>

Js code:

Js代码:

function doSomething(obj){
alert($(obj).closest('.parent').find(".something-inner").data("id"););
}

working fiddle

工作小提琴

#2


0  

    <div class="parent">
        <div class="something">
            <div class="something-inner" data-id="xyz">
                some contents
            </div>
        </div>
        <div class="next">
            <a onclick="doSomething(this);"></a>
        </div>
    </div>
<script>

    function doSomething(obj)
    {
          console.log($(obj).parent().prev().find(".something-inner").attr("data-id"));
    }

</script>

#3


-1  

Basically .parent() will select the element's immediate parent, You should use .closest() to achieve the result of your expectation.

基本上.parent()将选择元素的直接父级,您应该使用.closest()来实现期望的结果。

Try,

尝试,

HTML:

HTML:

<a onclick="doSomething(e);"></a>

JS:

JS:

$(e).closest(".parent").find(".something-inner").data("id");

#4


-1  

try something like this

尝试这样的事情

html

HTML

<a onclick="doSomething(this);"></a>

javascript

JavaScript的

function doSomething(obj){
    $(obj).parent(".parent").find(".something-inner").data("id");
}