admin管理员组文章数量:1330155
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
2 Answers
Reset to default 5First, 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
版权声明:本文标题:javascript - Dynamically populating <li> within jQuery Mobile data-role=listview - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268982a2443945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论