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

前端 未结 7 806
长发绾君心
长发绾君心 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条回答
  • 2020-12-13 13:31

    Yes, you can do it easily!

    0 讨论(0)
  • 2020-12-13 13:41

    No javascript solution or it depends on bootstrap's js already in use, just exploiting the DOM structure-

    See the data-target=""...

    A hint for easy way to reduce the bulky solutions-

    <button
    data-toggle="collapse"
    data-target=".dropdown-toggle:hover + .more-menu"
    type="button"
    class="btn btn-primary dropdown-toggle"
    >
      Toggle Bagal Wala
    </button>
    <div class="collapse more-menu">I will be toggled</div>
    

    This CSS selection structure will select the desired DOM .dropdown-toggle:hover + .more-menu and there we can apply our desired CSS. There are more ways to exploit what we have. :hover or :active or so many other ways.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 13:53

    @merv's solution didn't work for me in IE9 and below, since the collapsible state wasn't available unless you clicked at each item once. It did work fine in Firefox and Chrome though. So after two clicks, everything would work.

    What I did was set a .collapse-next class to the triggering elements, then force their ul siblings to collapse with toggle set to false:

    $(".collapse-next").closest('li').each(function(){
      if ($(this).hasClass('active')) {
        // pop up active menu items
        $(this).children("ul").collapse('show');
      } else {
        // just make it collapsible but don't expand
        $(this).children("ul").collapse({ toggle: false });
      }
    });
    

    This is for actually toggling the menu state:

    $('.collapse-next').click(function(e){
      e.preventDefault();
      $(this).parent().next().collapse('toggle');
    });
    

    It seems that using data- attributes is a somewhat more modern and cleaner approach, but for old browsers working with classes and jQuery seems to do the job as well.

    0 讨论(0)
  • 2020-12-13 13:54

    While it is true that the selector in a data-target attribute is a jQuery selector, the data-api specification for this plugin provides no means of referencing back to this in the scope of execution (see lines 147-153 in bootstrap-collapse.js for its use).

    However, I would like to offer another alternative approach, which is to extend the data-api with your own custom toggle specifier. Let's call it collapse-next.

    JS (see update note)

    $('body').on('click.collapse-next.data-api', '[data-toggle=collapse-next]', function (e) {
      var $target = $(this).parent().next()
      $target.data('collapse') ? $target.collapse('toggle') : $target.collapse()
    })
    

    HTML

    <a class="accordion-toggle" data-toggle="collapse-next">
    

    JSFiddle (updated)

    Downside here is that it's a rather tightly coupled approach, since the JS presumes a specific structure to the markup.


    Note about IE issues

    As @slhck pointed out in his answer, IE9 and under apparently fail on the first click when using an earlier revision of my answer. The cause is actually not an IE issue at all, but rather a Bootstrap one. If one invokes .collapse('toggle') on a target whose Carousel object is uninitialized, the toggle() method will be called twice - once during initialization and then again explicitly after initialization. This is definitely a Bootstrap bug and hopefully will get fixed. The only reason it doesn't appear as a problem in Chrome, FF, IE10, etc, is because they all support CSS transitions, and hence when the second call is made it short-circuits because the first one is still active. The updated workaround above merely avoids the double-call problem by checking for initialization first and handling it differently.

    0 讨论(0)
  • 2020-12-13 13:57

    TYPO3 FCE and Bootstrap Accordion

    I am having some trouble with this issue too i am using it in TYPO3 for a customer who wants to be able to add an infinite number of elements to the accordion. So I created a Flexible Content Element and mapped the elements.

    The idea with that data-toggle="collapse-next" did not work for me as expected as it did not close the open elements. I created a new javascript-function doing that please find the code here. Hopefully someone finds the stuff useful.

    Javascript

    $(document).on('click.collapse-next.data-api', '[data-toggle=collapse-next]', function (e) {
      var $container = $(this).parents(".accordion");
      var $opencontainers = $container.find(".in");
      var $target = $(this).parent().next();
      $target.data('collapse') ? $target.collapse('toggle') : $target.collapse();
      $opencontainers.each(function() {$(this).collapse('toggle')});
    })
    

    HTML

    <html>
        <div class="accordion">
            <div class="accordion-section">
                <div class="accordion-group">
                    <div class="accordion-heading">
                        <a class="accordion-toggle" data-toggle="collapse-next">
                            Collapsible Group Item #1
                        </a>
                    </div>
                    <div class="accordion-body collapse">
                        <div class="accordion-inner">
                            Anim pariatur cliche...
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </html>
    
    0 讨论(0)
提交回复
热议问题