【问题标题】:Adapting load() code调整 load() 代码
【发布时间】:2013-04-30 18:51:06
【问题描述】:

我正在尝试修改 post 中的代码,解决方案是:

function animateTransition(event) {
    // load the html contained in the tag with id of #main
    $('#article-container').load('myArticle.html #main', function() {
        // load the css
        $('head').load('myCSS.css', function() {
            // load the js
            $.getScript('myScript.js', function() {
                console.log("Animating...");
                $('#content-container').addClass('animate');
                $('#article-container').addClass('animate');
            });
        });
    });
}

...想出我自己的版本:

<div id="menu_aEmpresa"
     onclick="$('#mainContent')
              .load('aEmpresa_pt.html #aEmpresa_mainContent > *', function(){
                $('head').load('css/aEmpresa.css'); // load the css
                $.getScript('js/aEmpresa.js'); // load the js
              });">
</div>

基本上,我想在加载内容的同时加载外部文件 (aEmpresa_pt.html) 中引用的 css 和 js 文件。 但是,这种语法不起作用。请帮忙?

谢谢。

佩德罗

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这不是您应该使用 jquery 编码的方式。

    您应该将您的 html 代码与您的 js/jquery 代码分开:

    HTML

    <div id="menu_aEmpresa"></div>
    

    jQuery

    <script>
        $(function(){
            $('#menu_aEmpresa').on('click',function(){
                $('#mainContent')
                  .load('aEmpresa_pt.html #aEmpresa_mainContent > *', function(){
                    $('head').load('css/aEmpresa.css'); // load the css
                    $.getScript('js/aEmpresa.js'); // load the js
                 });">
            });
        });
    </script>
    

    【讨论】:

    • 感谢您的快速响应。问题仍然存在,内容加载正常,但没有应用 css 和 js。文件名和路径都很好(不过必须在文件夹前放一个“/”)......有什么想法吗?
    猜你喜欢
    • 2017-07-08
    • 2012-06-07
    • 1970-01-01
    • 2015-02-05
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多