带有onsen ui的javascript:动态添加元素

带有onsen ui的javascript:动态添加元素,javascript,onsen-ui,Javascript,Onsen Ui,我想将一些html元素添加到已经定义的onsen ui元素中,有时有效,有时无效,代码中不起作用的部分肯定有问题,但我无法确定问题所在 这是我的密码: <ons-template id="presentation.html"> <ons-page id ="pre"> <ons-toolbar> <div class="left"> <ons-toolbar-button

我想将一些html元素添加到已经定义的onsen ui元素中,有时有效,有时无效,代码中不起作用的部分肯定有问题,但我无法确定问题所在

这是我的密码:

<ons-template id="presentation.html">
      <ons-page id ="pre">
        <ons-toolbar>
          <div class="left">
            <ons-toolbar-button onclick="menu.toggleMenu()">
             <img src = "img/icon.png"/>
            </ons-toolbar-button>
          </div>
          <div class="center">mysql content</div>
        </ons-toolbar>

        <h1 style="text-align: center" id= "content">Page2</h1>


      </ons-page>
    </ons-template>


<ons-template id="menu.html">
  <ons-list id = "list">
    <ons-list-item modifier="chevron" onclick="menu.setMainPage('home.html', {closeMenu: true});
                                              getPageContent(1);
                                             ">

      Presentation 1
    </ons-list-item>
    <ons-list-item modifier="chevron" onclick="menu.setMainPage('presentation.html', {closeMenu: true});
                                               loadPresentation(0);
                                              ">
      Presentation Mysql
    </ons-list-item>
    <ons-list-item modifier="chevron" onclick="menu.setMainPage('page2.html', {closeMenu: true});
                                               ">
      Test
    </ons-list-item>
  </ons-list>
</ons-template>


<script>
ons.ready(function() {

  menu.on('postclose', function() {
    loadPresentation(0);
    addElement();
});
</script>
当我这样做的时候

var onspage = document.getElementById("content");
               onspage.appendChild(p);
它工作正常。但是我的元素被添加了两次

这个代码也很好用:

var instaItem = document.createElement('ons-list-item');
  instaItem.setAttribute('modifier', "chevron");
    instaItem.setAttribute('onclick', "");

  instaItem.innerHTML = "press me";
  document.getElementById("list").appendChild(instaItem);
有人帮忙吗

var instaItem = document.createElement('ons-list-item');
  instaItem.setAttribute('modifier', "chevron");
    instaItem.setAttribute('onclick', "");

  instaItem.innerHTML = "press me";
  document.getElementById("list").appendChild(instaItem);