JQuery-Mobile collapsible expand/collapse event

前端 未结 5 919
离开以前
离开以前 2020-12-01 15:53

Does anyone know any other way to capture the event of expanding or collapsing a component marked with data-role=\"collapsible\" apart from the onclick

相关标签:
5条回答
  • 2020-12-01 16:44

    You can bind any event you want, example:

    • http://jsfiddle.net/9mDbh/ // Events
    • http://jsfiddle.net/9mDbh/1/ // Touch Events

    Demo:

    • http://jsfiddle.net/9mDbh/5/

    JS

    $("div:jqmData(role='collapsible')").each(function(){
        bindEventTouch($(this)); 
    });
    
    function bindEventTouch(element) {
        element.bind('tap', function(event, ui) {
           if(element.hasClass('ui-collapsible-collapsed')) {
                alert(element.attr('id')+' is closed');
            } else {
                alert(element.attr('id')+' is open');
            }
        });
    }
    

    HTML

    <div data-role="page" id="home">
        <div data-role="content">
            <div data-role="collapsible" id="number1">
               <h3>Header #1</h3>
               <p>I'm Header #1</p>
            </div>
            <br />
            <div data-role="collapsible" data-collapsed="false" id="number2">
               <h3>Header #2</h3>
               <p>I'm Header #2</p>
            </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-01 16:45

    I don't think there is another way to do this. I do not believe that you can utilize animationComplete in this case as described here:
    http://jquerymobile.com/test/docs/api/events.html

    Maybe you can utilize certain class changes on the specific elements, which in fact isn't better than binding an onclick event of the header.

    0 讨论(0)
  • 2020-12-01 16:47

    You could try the jQuery mobile tap event. I haven't used it personally, but I hear it's similar to the mousedown event handler. If you gave a little more detail about why the onclick isn't working maybe we could help you out a little more.

    0 讨论(0)
  • 2020-12-01 16:48

    There are custom events for the collapsible blocks. You can bind to the expand and collapse events:

    $('#my-collapsible').bind('expand', function () {
        alert('Expanded');
    }).bind('collapse', function () {
        alert('Collapsed');
    });
    

    Here is a jsfiddle: http://jsfiddle.net/6txWy/

    0 讨论(0)
  • 2020-12-01 16:54

    In jQuery Mobile 1.4 there are the collapsiblecollapse and collapsibleexpand events. https://api.jquerymobile.com/collapsible/#event-collapse

    $( ".selector" ).on( "collapsiblecollapse", function( event, ui ) {} );
    
    0 讨论(0)
提交回复
热议问题