【问题标题】:How to add code dinamically with JQuery Mobile?如何使用 JQuery Mobile 动态添加代码?
【发布时间】:2016-11-29 17:00:45
【问题描述】:

我正在尝试将一些动态生成的代码添加到使用 Jquery Mobile 的现有 HTML 文件中:

<div data-role="page" id="page">
Click on the button
    <a href="#section" id="abutton" data-role="button">Add item</a>
    <label for="id" id="id-label">ID:</label>
    <input type="text" id="id" name="id" value="" />
    <label for="name" id="name-label">Name:</label>
    <input type="text" id="name" name="name" value="" />
</div>

问题是当我添加一些代码时,新代码与现有代码的外观不同:

 $('#abutton').click(function(){
        var code = '<label for="surname" id="surname-label">Surname:</label>' +
                   '<input type="text" id="surname" name="surname" value="" />';
        $("#page").append(code);
        return false;
 });

我一直在研究,发现非动态生成的 HTML 代码添加了一些标签和类。我想我必须调用一些函数来解析新的动态生成的代码,但是哪个?还是我应该做一些完全不同的事情?

Demo here:我添加了一个动态生成的来显示 jquery mobile 生成的其他结构,仅添加一些类就无法解决。

【问题讨论】:

  • $("#page").append(code).enhanceWithin();
  • 是的@Omar,这就是答案!请把它放在一个答案中以接受它作为一个好的答案:)

标签: jquery html jquery-mobile dynamically-generated


【解决方案1】:

您缺少由 jquery mobile 最初添加到标签和输入字段的 ui 类。

但是当您添加新标签和输入字段时,它不会获得这些类。所以你需要在添加这些字段时添加类。

 $('#abutton').click(function(){
        var code = '<label for="surname" id="surname-label" class="ui-input-text">Surname:</label>' +
        					 '<input type="text" id="surname" name="surname" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" />';
        $("#page").append(code);
        return false;
 });

【讨论】:

  • Mmmhhhh...我已经测试了您的代码,但仍然无法正常工作:/
  • 工作,但是,这是唯一的方法吗?看起来太黑了,因为您必须了解可能会改变的 jquery.mobile 的内部结构......
  • 如果你的
猜你喜欢
  • 2011-11-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多