问题
There is a list view and this is loaded dynamically upon loading the page. This adds elements inside list view. The problem is i have placed backbutton.After going to previous screen and returning back to current screen it is loading the data and appending to the List View.
I need to remove the <li> elements from the list view.
The HTML code snippet.
<ul id="mymenu" data-role="listview" >
</ul>
Jquery Code Snippet.
$("#mypmenu").append('<li><a href='+ "#" + ' id="a" "> <img src="letterheader.png" >'+ this.textContent + ' </a> </li>');
Now i need to remove the elements from the list view (mymenu)which are loaded already.
回答1:
Try emptying your list before appending list items first. Afterwards, make sure to call the refresh function of the listview widget, so jQuery Mobile will be rendering your list correctly.
$("#mypmenu").empty().append('<li><a href='+ "#" + ' id="a" "> <img src="letterheader.png" >'+ this.textContent + ' </a> </li>').listview("refresh");
See also http://forum.jquery.com/topic/dynamically-generated-listview
回答2:
$("mypmenu").empty(), followed by $("mypmenu").append()
does the job for me
回答3:
this work for me
$("mypmenu li").remove();
回答4:
Don't use append then.
use
$("#mypmenu").html('<li><a href='+ "#" + ' id="a" "> <img src="letterheader.png" >'+ this.textContent + ' </a> </li>').listview("refresh");
来源:https://stackoverflow.com/questions/8727272/how-to-remove-elements-from-listview-jquery-mobile