JQuery Mobile附加新按钮的行为不符合预期

时间:2012-07-10 14:52:44

标签: javascript jquery jquery-mobile

  

可能重复:
  jQuery Mobile does not apply styles after dynamically adding content

我想知道我在这里错过了什么......

如果我写

<a onclick="deleteThis()" data-role="button" data-theme="a">Button text</a>

在HTML页面的正文中,它可以正常工作。 但是当我以编程方式尝试使用以下内容添加新按钮时:

$('.block').append('<a onclick="deleteThis()" data-role="button" data-theme="a">' + results[i].Title + '</a>');

链接附加正常,没有JQuery移动样式,data-theme =“a”应该将特定颜色样本css应用于相关元素。

我哪里错了?

3 个答案:

答案 0 :(得分:5)

 $('.block').append(' <a onclick="deleteThis()" data-role="button" data-theme="a"> Text</a>').trigger( "create" );

答案 1 :(得分:0)

您可以使用jQuery Mobile提供的.buttonMarkup()方法。此方法也允许您传递一些可选参数。

以下是.buttonMarkup()方法的文档:http://jquerymobile.com/demos/1.1.0/docs/buttons/buttons-options.html

以下是使用此方法创建按钮的示例:

$('<a href="#" />').text('New Button').buttonMarkup({
    theme  : 'a',
    icon   : 'star',
    mini   : false,
    inline : false
});
//these are just a few of the options

以下是演示:http://jsfiddle.net/jasper/6BF6M/

另外,我可以建议使用jQuery来制作事件绑定,例如:

$('.block').append(
    $('<a href="#" />').text('New Button').buttonMarkup().bind('click', deleteThis)
);

答案 2 :(得分:-1)

尝试触发页面的刷新事件:

$('#pageId').trigger('pagecreate');

如果是列表视图:

$('#listId').listview('refresh');
相关问题