简体   繁体   中英

Dynamically add jquery mobile components to listview with formatting

When I'm dynamically inserting the following code, I can force the page to refresh so it applies the jQuery mobile formatting.
For some reason its doesn't allow me to set the button formatting. Its really odd, because it allows the list to be dynamically inserted and the button, but i cant seem to format the button except for the name.
It can be manipulated via CSS but I want to use the jQuery API .

$('#cartList').append('<li>'
    + '<h3>' + game.Title+'</h3>'
    + '<p>' + '£' + game.Price + '</p>' 
    + '<a href="#cartList" data-role="button" data-icon="none" data-inline="true" onclick="removeItem()">Remove</a>'
    + '<h3 class="ui-li-aside">' + game.Format + '</h3>'
    + '</li>').trigger("create");

The jQuery Mobile Doc mentions that if new list items are added to the list or removed from it, the dividers are not automatically updated and you should call refresh() on the listview to redraw the autodividers.

Try to add: $('#cartList').listview('refresh'); after you have populated the list.

Example:

<!doctype html>
<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>   

         <script>
            $(document).ready(function(){
                $("#add-li-button").click(function(){ 
                    $('#listList').append("<li data-role=\"collapsible\"> <h3>New List</h3> <div data-role=\"fieldcontain\"></div> </li>").listview("refresh");
                });
            });
        </script>   
   </head>
   <body>
        <div data-role="page" id="page">
            <ul id="listList" data-role="listview">
            </ul>
            <input type="button" id="add-li-button" value="add a checkbox to list1" />
        </div>    
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM