简体   繁体   English

Bootstrap 4 使用 e.relatedTarget 查找元素

[英]Bootstrap 4 find element with e.relatedTarget

Hi trying to get data attr after e.relatedTarget in bootstrap's carusel.嗨,试图在引导程序的轮播中的 e.relatedTarget 之后获取数据属性。

$('#carouselExampleControls').on('slide.bs.carousel', function(e){
        target = e.relatedTarget

    });

when i do console.log got this当我做 console.log 得到这个

<div data-title="[555]" class="carousel-item active">
   <img class="d-block w-100" data-children="[ data-parent-id="" data-id="977" src="http://localhost/pics/8.png" alt="8.png">
   </div>

Trying to get data-title, type of this is object, tried with $.each not sure how to parse it.尝试获取数据标题,类型为 object,尝试使用 $.each 不知道如何解析它。

To get the value of an attribute from any html element do the following:要从任何 html 元素中获取属性值,请执行以下操作:

var element = document.querySelector('.carousel-item.active');
var title = element.getAttribute('data-title');

considering your code, and using jquery:考虑您的代码,并使用 jquery:

$('#carouselExampleControls').on('slide.bs.carousel', function(e){
    var target = e.relatedTarget
    var title = $(target).attr('data-title'); // same as target.getAttribute('data-title');
});

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

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