jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用jQuery从CDN加载Dojo大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我从 article获得的一个例子,有点改变.这个例子非常有效

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" media="screen">
        <script type="text/javascript">
            dojoConfig = {
                parSEOnLoad: false,async: true
            };
        </script>   
        <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js" type="text/javascript"></script>
        <script type="text/javascript">
            /// Require the registry,parser,Dialog,and wait for domReady
            require(["dijit/registry","dojo/parser","dojo/json","dojo/_base/config","dijit/Dialog"],function (registry,JSON,config) {
                // Explicitly parse the page
                parser.parse();
                // Find the dialog
                var dialog = registry.byId("dialog");
                // Set the content equal to what dojo.config is
                dialog.set("content","<b>it works!</b>");
                // Show the dialog
                dialog.show();
            });
        </script>
    </head>
    <body class="claro">
        <div id="dialog" data-dojo-type="dijit.Dialog"></div>
    </body>
</html>

现在我想修改它并使用jQuery动态加载Dojo.以下是我如何执行此操作的示例:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" media="screen">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            dojoConfig = {
                parSEOnLoad: false,async: true
            };

            $.getScript("http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js")
            .done(function (script,textStatus) {
                /// Require the registry,and wait for domReady
                require(["dijit/registry",config) {
                    // Explicitly parse the page
                    parser.parse();
                    // Find the dialog
                    var dialog = registry.byId("dialog");
                    // Set the content equal to what dojo.config is
                    dialog.set("content","<b>it works!</b>");
                    // Show the dialog
                    dialog.show();
                });
            })
            .fail(function (jqxhr,settings,exception) {
                alert('Cannot load Dojo.js');
            });
        });
    </script>
</head>
<body class="claro">
    <div id="dialog" data-dojo-type="dijit.Dialog">
    </div>
</body>
</html>

但看起来我做错了,因为它引发了下一个错误

NotFoundError: Node was not found
http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
Line 2

我怀疑Dojo还没有准备好,但也许我错了……是否有可能使用jQuery来动态加载Dojo?

解决方法

“找不到节点”错误是由加载程序尝试找到加载它的脚本标记引起的.这是Dojo从CDN(如您使用过的Google)加载它时尝试查找加载模块的url路径时使用的技巧.

jQuery $.getScript()函数实际上并没有将脚本标记注入页面,而是通过XHR加载然后评估代码.因此,找不到Dojo正在寻找的标签.这仅在使用CDN时发生.如果您使用自己的Dojo本地副本而不是CDN,则可以使其工作.

我不确定通过jQuery加载Dojo是一种很好的做法.最好分别加载它们或者反过来加载它们(即在Dojo中加载jQuery).我假设你需要两者的功能,否则你不会尝试这个.

要将jQuery作为Dojo模块加载,您可以按如下方式更改代码

<!DOCTYPE HTML>
<html lang="en">
<head>
    <link
        rel="stylesheet"
        href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css"
        media="screen"
    />
    <script type="text/javascript">
        var dojoConfig = {
            "parSEOnLoad": false,"async": true,"packages": [{
                    "name": "jquery","location": "//ajax.googleapis.com/ajax/libs/jquery/1.9.0","main": "jquery.min"
                }]
        };
    </script>
    <script
        type="text/javascript"
        src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"
    ></script>
    <script type="text/javascript">
        define.amd.jQuery = true;  // jQuery will be loaded as an AMD module

        require([
            "jquery",],function($){
            // NB: $is only available within the scope it has been loaded
            // as it is loading as an AMD module.  Hence,$is not globally
            // available and must be required into any scope it is used.

            $(document).ready(function () {
                require([
                    "dijit/registry","dijit/Dialog"
                ],"<b>it works!</b>");
                    // Show the dialog
                    dialog.show();
                });
            });
        })
    </script>
</head>
<body class="claro">
    <div id="dialog" data-dojo-type="dijit/Dialog">
    </div>
</body>
</html>

坚持使用Dojo可能更好,而不是试图同时使用它们.但是,上述内容将允许两者一起使用. Dojo,它有自己的就绪功能(dojo/ready),可以取代$(document).ready(). jQuery的大多数功能都在Dojo中的某个庄园中复制.

将jQuery作为Dojo模块加载意味着它仅在require回调中可用.因此,$通常不会放在全局范围内.您将需要将其用于您需要的任何JavaScript.

注意:我将你的代码中的dijit.Dialog更改为dijit / Dialog,因为如果使用点形式,它将不会加载到1.8版本中.

@H_419_54@

大佬总结

以上是大佬教程为你收集整理的使用jQuery从CDN加载Dojo全部内容,希望文章能够帮你解决使用jQuery从CDN加载Dojo所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: