admin管理员组

文章数量:1330408

While using jQuery Mobile <ul data-role="listview">, I am trying to add some <li>s from JavaScript, but the new <li> is not inheriting the jQuery Mobile styles. Here is the code:

<head>
  <script>
    function init()
    {
      msg = "<li> <a href=> New List Item </a></li>";
      document.querySelector('#add_item').innerHTML +=  msg;
    }

    // call init on load
    window.addEventListener('load',init,false);
  </script>
</head>

<body>
  <div id='main' data-role='page' data-theme='c'>
    <div data-role='header'>
      <h1>Wele!</h1>
    </div>
    <div id='content' data-role='content'>
      <ul data-role="listview" id="list_cars">
        <div id="add_item">
        </div>

        <li> <a href=""> List Item 1</a></li>
        <li> <a href=""> List Item 1</a></li>      
      </ul>
    </div>
    <div data-role='footer'>
      <h4>Enjoy reading the book ...</h4>
    </div>
  </div>  <!-- End of Min page -->
</body>

While using jQuery Mobile <ul data-role="listview">, I am trying to add some <li>s from JavaScript, but the new <li> is not inheriting the jQuery Mobile styles. Here is the code:

<head>
  <script>
    function init()
    {
      msg = "<li> <a href=> New List Item </a></li>";
      document.querySelector('#add_item').innerHTML +=  msg;
    }

    // call init on load
    window.addEventListener('load',init,false);
  </script>
</head>

<body>
  <div id='main' data-role='page' data-theme='c'>
    <div data-role='header'>
      <h1>Wele!</h1>
    </div>
    <div id='content' data-role='content'>
      <ul data-role="listview" id="list_cars">
        <div id="add_item">
        </div>

        <li> <a href=""> List Item 1</a></li>
        <li> <a href=""> List Item 1</a></li>      
      </ul>
    </div>
    <div data-role='footer'>
      <h4>Enjoy reading the book ...</h4>
    </div>
  </div>  <!-- End of Min page -->
</body>
Share Improve this question edited Apr 9, 2013 at 13:40 Travis 12.4k8 gold badges43 silver badges53 bronze badges asked Apr 8, 2013 at 18:04 user2041129user2041129 211 silver badge4 bronze badges 1
  • Can you please clarify your question? I'm not sure what the problem is (what's "JSQUERY styles")? – Alfred Xing Commented Apr 8, 2013 at 18:08
Add a ment  | 

2 Answers 2

Reset to default 5

First, you're putting your <li> inside a <div> instead of directly inside the <ul>. Try changing your querySelector to...

document.querySelector('#list_cars').innerHTML +=  msg;

or, if you want the item at the top of the list, use this...

document.querySelector('#list_cars').innerHTML = msg + document.querySelector('#list_cars').innerHTML;

Then, you need to tell jQuery Mobile to update that list view by adding...

$('#list_cars').listview('refresh');

The last section on this jQuery Mobile Documentation page about lists describes this feature.

Working example here: http://jsbin./omepob/1/

To apply the properties on the new object, the optimal way is to call jQuery's refresh method for the object:

$("#myobject").listview("refresh");

However, I encourage you to replace this innerHTML call for something more DOM-atic, like:

var myli = document.createElement("li");
myli.appendChild(document.createTextElement("List Item 3"));

BTW, agreed with Travis; better include a "ul" as a parent of "li" instead of using a "div" there...

本文标签: javascriptDynamically populating ltligt within jQuery Mobile datarolelistviewStack Overflow