简体   繁体   English

JavaScript:动态注入和删除html页面

[英]JavaScript: Dynamically injecting and removing html pages

I'm developing a Phonegap/Cordova application and having serious performance issues. 我正在开发Phonegap / Cordova应用程序,并且遇到严重的性能问题。 I'm looking into a tutorial and the author is recommending dynamic creation of html pages using JavaScript. 我正在研究一个教程,作者建议使用JavaScript动态创建html页面。

here's a link of the tutorial: http://coenraets.org/blog/phonegap-tutorial/ 这是教程的链接: http : //coenraets.org/blog/phonegap-tutorial/

scrolling down to part 4: "A single page application is a web application that lives within a single HTML page. The 'views' of the application are injected into- and removed from the DOM as needed as the user navigates through the app. " 向下滚动至第4部分:“单个页面应用程序是一个驻留在单个HTML页面中的Web应用程序。当用户浏览该应用程序时 ,该应用程序的“视图”将根据需要注入到DOM中或从DOM中删除。

I understand the creation part, but how are the pages being removed from the DOM??? 我了解创建部分,但是如何从DOM中删除页面?

Code by Christopher Coenraets: Christopher Coenraets的代码:

renderHomeView: function() {
  var html =
        "<div class='header'><h1>Home</h1></div>" +
        "<div class='search-view'>" +
        "<input class='search-key'/>" +
        "<ul class='employee-list'></ul>" +
        "</div>"
  $('body').html(html);
  $('.search-key').on('keyup', $.proxy(this.findByName, this));
},

The initialize function: 初始化函数:

initialize: function() {
  var self = this;
  this.store = new MemoryStore(function() {
     self.renderHomeView();
  });
}

I don't see any function that removes the HomeView after rendering it. 我没有看到渲染HomeView后会删除HomeView的任何功能。

please help me figure this out as I've been going over it for days 我已经解决了好几天,请帮我解决这个问题

first, create an *.html page with your content. 首先,使用您的内容创建一个* .html页面。 store it on root as /html/homeview.html 将其存储在/html/homeview.html的根目录下

homeview.html homeview.html

    <div class='header'>
       <h1>Home</h1>
    </div>
       <div class='search-view'>
       <input class='search-key'/>
       <ul class='employee-list'></ul>
    </div>

index.html, you've : index.html,您已经:

     <div class="homeView"></div>

And implement the jQuery ajax or simple $.get() : 并实现jQuery ajax或简单的$ .get():

  $(document).ready(function() {
     $.get('html/homeView.html')
         .done(function(data) { //this is homeView's HTML content
             $('.homeView').html(data);
         })
         .fail(function(error) {
             console.log(error);
         })
   })

remove its content by 通过删除其内容

    $('.homeView').html('');

The JQuery method "$('body').html(string);" jQuery方法“ $('body')。html(string);” sets the content of the 'body' tag in the html code to whatever the string specifies. 将html代码中'body'标签的内容设置为字符串指定的内容。 Meaning previous content inside the body tag will be removed from the DOM. 这意味着body标记内的先前内容将从DOM中删除。

You can also remove specific elements from the DOM using another JQuery method called remove(). 您还可以使用另一个称为remove()的JQuery方法从DOM中删除特定元素。 This is the method he uses later in the tutorial when implementing sliding between html pages. 这是他稍后在html页面之间实现滑动时使用的方法。

There are updated versions of the tutorial you linked to http://ccoenraets.github.io/cordova-tutorial/create-cordova-project.html . 您链接到http://ccoenraets.github.io/cordova-tutorial/create-cordova-project.html的教程有更新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM