简体   繁体   English

jQuery-如何从e.target获取嵌套DIV的ID?

[英]Jquery - how do I get the ID of a nested DIV from an e.target?

This code: 这段代码:

$('.grid-stack').on('resizestop', function(e) {
        var element = e.target;
        console.log(element);
 });

outputs: 输出:

<div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide" data-gs-x="0" data-gs-y="2" data-gs-width="7" data-gs-height="2">
                <div class="grid-stack-item-content ui-draggable-handle">
                    <div class="box-prop top-widget" data-sortable-id="cars-sold-minute" id="cars-sold-minute-block">
                        <i class="fa fa-times close-x" aria-hidden="true" style="display: none;"></i>
                        <div class="single-pie">

To the console.log. 到console.log。 I am attempting to get the id which is cars-sold-minute-block using jquery but have not been successful. 我正在尝试使用jquery获取id为cars-sold-minute-block的ID,但未成功。 How do I get the id when using the event target? 使用事件目标时如何获取ID?

Edit: I don't want to name the id, as this will be for many different elements. 编辑:我不想命名ID,因为它将用于许多不同的元素。

You can get it by the element class 您可以通过元素类获得它

 $('.grid-stack').on('resizestop', function(e) {
        var element = e.target;
        var id = $(element).find('.box-prop.top-widget').attr('id');
        console.log(element);
 });

For More performance, use $ with two arguments where the 2nd argument is the CTX (parent) : 为了获得更高的性能,请在两个参数中使用$ ,其中第二个参数是CTX(父级):

 $('.grid-stack').on('resizestop', (e)=> {
        let element = e.target;
        var id = $('.box-prop.top-widget',element).attr('id');
        console.log(id);
 });

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

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