1

我尝试使用脚本中的一些“dijit”小部件,例如更改内容或连接事件。

为此,我尝试在 html 中使用“data-dojo-id”属性,它(据我了解)创建类型为“data-dojo-type”并命名为“data-dojo-id”的全局对象。

但我有错误......我做错了什么?

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Dojo test</title>
    </head>
    <body>
        <div    id="myDivId"
                data-dojo-type="dijit.layout.ContentPane"
                data-dojo-id="myDojoId">
        Hello Everyone!
        </div>
        <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js" data-dojo-config="async: true, parseOnLoad:true"></script>
        <script type="text/javascript">
        require(["dijit/dijit", "dijit/layout/ContentPane"], function(){
            //myDojoId.setContent("Hello World!");  // error : myDojoId is not defined

            // Even this not working:
            console.log(dijit.byId("myDivId"));     // undefined 
            console.log(dijit.byId("myDojoId"));    // undefined 
            });
        </script>
    </body>
</html>
4

1 回答 1

3

data-dojo-id和 jsId(已弃用)属性创建一个引用该 dijit 的全局对象。

我相信您需要将查找包装在 ready() 调用中

<script type="text/javascript">
       require(["dojo/ready","dijit/dijit", "dijit/layout/ContentPane"], function(ready,dijit,ContentPane){
            //shouldn't  be defined yet
            console.log(dijit.byId("myDivId"));     // undefined 
            console.log(myDojoId);    // undefined 

            ready(function(){
             console.log(dijit.byId("myDivId"));
             //note how data-dojo-id doesn't have the lookup
             console.log(myDojoId);
           });
        });
</script>
于 2012-05-29T13:29:03.143 回答