Dynamically Add Buttons Via JQuery Mobile

半腔热情 提交于 2020-01-03 20:11:34

问题


I am working on a JQuery Mobile app. I am dynamically adding HTML when the page loads. A trimmed down example is shown here:

var h = "";
for (i=1; i<=5; i++) {
  h += "<div>Entry #" + i + "</div>";
  h += "<div class='ui-grid-a'>";
    h += "<div class='ui-block-a'><input type='button' value='Approve' onclick='return approveButton_Click(this);' /></div>";
    h += "<div class='ui-block-b'><input type='button' value='Reject' onclick='return rejectAbuse_Click(this);' /></div>";
  h += "</div><hr />";
});
$("#entries", "#myPage").append(h);

My HTML is appearing in the UI. However, the buttons are not rendered as a typical JQuery Mobile buttons. Instead, they look like traditional HTML buttons. How do I get my dynamically added buttons to apply the JQuery mobile styling?

Thank you


回答1:


Here's an working example out of your code: http://jsfiddle.net/Gajotres/NuCs2/

Before you can refresh the button/s it first must be initialized with .button() function. Just like this:

$("#approve"+i).button().button('refresh');
$("#reject"+i).button().button('refresh');

There's also another solution but you should use it only if you are recreating a whole page:

$("#index").trigger("pagecreate");

And here's an example for the second solution: http://jsfiddle.net/Gajotres/mpFJn/

If you want to find out more about methods of markup enhancement take a look at my other ARTICLE, let me be transparent, it is my personal blog. Or find it HERE.




回答2:


Use "Refresh" method in Jquery mobile for dynamic appearance of Jqm UI...

http://jquerymobile.com/demos/1.2.0/docs/buttons/buttons-methods.html



来源:https://stackoverflow.com/questions/14293276/dynamically-add-buttons-via-jquery-mobile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!