【问题标题】:how to create a reusable javascript web-app from my HTML CSS and JS files如何从我的 HTML CSS 和 JS 文件创建可重用的 javascript web-app
【发布时间】:2014-02-08 04:07:30
【问题描述】:

我有一个包含 CSS 和 JS 文件的工作 HTML 文件,我想用它创建一个 Web 应用程序。

我没有任何经验或想法如何根据我所拥有的东西创建 web 应用程序。

我的代码不需要与服务器进行任何交互。

我从this site找到了这个指南

    (function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) { 
        /******* Load CSS *******/
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "style.css" 
        });
        css_link.appendTo('head');          

        /******* Load HTML *******/
        var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
        $.getJSON(jsonp_url, function(data) {
          $('#example-widget-container').html("This data comes from another server: " + data.html);
        });
    });
}

})(); // We call our anonymous function immediately

1.) 在加载 CSS 之前我明白了,但我没有明白代码在加载 HTML 文件时做了什么。

2.) 是否有可能用我所拥有的以及如何做一个 webapp..?

3.) 我知道 css html js jq..........了解 ajax 或其他任何东西对于创建 Web 应用程序是否重要。

谢谢。

【问题讨论】:

    标签: javascript jquery html css web-applications


    【解决方案1】:

    我不确定我是否理解你的问题,但如果我尝试描述这个脚本在做什么,也许会对你有所帮助?

    基本上,这个脚本(有点笨拙地 IMO)首先加载 jQuery,然后加载样式表 style.css,最后从 http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=? 检索数据。它假定您有一个 ID 为 "example-widget-container" 的文档元素,它将注入从 JSON 调用接收到的 html。

    如果你澄清了你到底在问什么,那么也许我或其他人可以为你提供更多帮助。

    【讨论】:

    • thnx 兄弟的回复,实际上我担心在上面的代码中包含我的 html 文件以进行加载。我对 JSON 调用一无所知。如果有任何其他方法可以用我所拥有的(.html、.css、.js)创建小部件,请指导我。谢谢。
    • /******* 加载 HTML *******/ var jsonp_url = "al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?"; $.getJSON(jsonp_url, function(data) { $('#example-widget-container').html("这个数据来自另一个服务器:" + data.html); });它写的“这个数据来自另一台服务器,但我不希望它来自服务器....我有.html文件....如何加载该html文件..?
    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多