javascript - jquery mobile动态添加元素无法正常工作

标签 javascript jquery jquery-mobile

我想动态添加 li 标签。
事件虽然添加

listview("refresh");

它似乎不适用jquery mobile css。 我试过了

listview().listview("refresh");

下面的代码是我写的。 请将下面的代码保存为 tmp.html 并在您的本地主机上尝试。

在我的环境中,推送 ADD btn 时没有错误。

<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
  $(document).bind('mobileinit', function() {
    $.mobile.changePage.defaults.changeHash = false;
    $.mobile.hashListeningEnabled = false;
    $.mobile.pushStateEnabled = false;
  });
</script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
  function onAddBtn() {
    $li = $('<li><p>a:</p><input type="text"><p>b:</p><input type="text"></li>');
    $("#SentenceList").append($li).listview("refresh");
  }

  function onReady() {
    $("#AddBtn").click(onAddBtn);
  }

  $(onReady);
</script>
<div data-role="page" id="AddPage">
  <header data-role="header" data-position="fixed" data-theme="b">
    <a data-role="button" data-rel="back" data-icon="back" style="background-color: gray;">Back</a>
    <h1>Add Memo</h1>
  </header>
  <section data-role="content">
    <ul id="SentenceList" data-role="listview">
      <li>
        <p>a:</p>
        <input type="text">
        <p>b:</p>
        <input type="text">
      </li>
      <li>
        <p>a:</p>
        <input type="text">
        <p>b:</p>
        <input type="text">
      </li>
    </ul>
  </section>
  <a data-role="button" data-icon="plus" id="AddBtn">ADD</a>
</div>

最佳答案

您可以告诉 jQM 在刷新 ListView 后增强输入:

$("#SentenceList").append($li).listview("refresh").enhanceWithin();

DEMO

关于javascript - jquery mobile动态添加元素无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370929/

相关文章:

javascript - 在 reactjs 中更改 scrollTop

javascript - 将间隔集拆分为最小的不相交间隔集

javascript - 获取 https ://ipinfo. io/401(未经授权)

click - Jquery 单击事件不适用于带有按钮的覆盖层

javascript - 如何使用 jQuery 从 Web Worker Thread 中解析 XML

注入(inject)函数中的 Javascript 作用域

javascript - 使用jQuery单击页面上的任意位置关闭下拉菜单

javascript - jQuery Mobile - 保存用户输入的最佳方式是什么?

ios - JqueryMobile-页面事件变为事件状态

json - 使用 JQuery Mobile 和 Json 动态注入(inject)页面