Can you specify a “data-target” for Bootstrap which refers to a sibling DOM element without using an ID?

前端 未结 7 808
长发绾君心
长发绾君心 2020-12-13 13:04

I am dynamically adding Collapsable elements to a page. Bootstrap uses the \"data-target\" attribute to specify which element the collapse toggle applies to.

From

7条回答
  •  -上瘾入骨i
    2020-12-13 13:48

    I think the best approach would be to do this iterate all accordion-toggle elements and set their data-target attribute dynamically via jquery and after that place the code of accordion mentioned in bootstrap.

    Example :

    $(function(){
        $("a.accordion-toggle").each(function(index) {
            $(this).attr("data-target", "#" + $(this).parent().next().attr("id"));
        });
    
        // accoridon code
    });
    

    Hope this will help

提交回复
热议问题