jQuery mobile error “cannot call methods on listview prior to initialization”

前端 未结 7 1056
北海茫月
北海茫月 2020-12-25 13:11

I\'m dynamically filling a

    then calling location.href=\"#Results\" where the list is, and finally listview(
相关标签:
7条回答
  • 2020-12-25 13:44

    simply add listview.refresh works fine for me,and I'm using ajax to load content into the div too.

    document.getElementById("myListview").innerHTML = xmlhttp.responseText;
    //works fine on my work
    $('#myListview').listview('refresh');
    

    here if my post.

    jquery mobile ajax load content into a div element will lose it's css style

    I spend almost 3 hours to solve my post probem.finaly find the answer here.thanks.

    0 讨论(0)
  • 2020-12-25 13:47

    I had the same error. I solved it by adding ":visible" to my query, so it will only run if the list is visible.

    So your code will look something like this:

    $('#myListview:visible').listview('refresh');
    

    Worked fine for me!

    0 讨论(0)
  • 2020-12-25 13:50

    you should check if it is already initialized or not, refresh the list in case it is initialized otherwise trigger create as per the following :

    if ( $('#myListview').hasClass('ui-listview')) {
        $('#myListview').listview('refresh');
         } 
    else {
        $('#myListview').trigger('create');
         }
    
    0 讨论(0)
  • 2020-12-25 13:50

    This is what worked for me:

       $(document).delegate('#Results', 'pageshow', function (){
       $('#mylistview').listview('refresh').trigger('create'); 
       });
    
    0 讨论(0)
  • 2020-12-25 13:56

    http://jquerymobile.com/demos/1.1.0/docs/api/events.html You have to hook on the pageinit event. You can't call any JQM methods prior to this. i.e.:

    $('#Results').bind('pageinit', function() {
      $('#myListview').listview('refresh');
    });
    
    0 讨论(0)
  • 2020-12-25 14:02

    use $.mobile.changePage("#Results"); instead of location.href
    actually location.href reload the page so the list view gets destroy

    And then listview.refresh

    0 讨论(0)
提交回复
热议问题