1
    <script>

require(["dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"], function(dom, domConstruct, on) {
             domConstruct.empty(dom.byId("buttonNode"));
        });

    var count=1;
    function change() {
        if(count == 1) {
        require(["dojo/ready", "dijit/form/Button", "dojo/dom"], function(ready, Button, dom){
        ready(function(){
            var myButton = new Button({
                label: "Count 1",
                onClick: function(){
                    count++;
                    change();
                }
            }, "buttonNode");
        });
    });
        } else if(count ==2) {
            require(["dojo/ready", "dijit/form/Button", "dojo/dom"], function(ready, Button, dom){
                ready(function(){
                    var myButton = new Button({
                        label: "Count 2",
                        onClick: function(){
                            count++;
                            change();
                        }
                    }, "buttonNode");
                });
            });
        }
    }
    </script>
    </head>
    <body class="claro" onload="change()">
        <button id="buttonNode" type="button" onclick="change()"></button>
    </body>
    </html>

单击按钮时,它的显示尝试使用 id==buttonNode 注册小部件,但该 id 已注册,但我在使用 domConstruct.empty 创建新按钮之前删除旧按钮。我尝试了 domConstruct.empty("buttonNode"); domConstruct.empty(dom.byId("buttonNode"));

我想要为不同的登录用户提供不同的按钮,例如发送请求、取消请求等,所以我正在检查条件并创建新按钮。问题是如果按钮已经用那个 ID 创建了我无法替换按钮请帮助我。

4

2 回答 2

2

我认为在注册表中使用已经创建的小部件比删除它并再次创建它更好:

    } else if(count ==2) {
        require(["dojo/ready", "dijit/form/Button", "dojo/dom", "dijit/registry"], function(ready, Button, dom, registry){
            ready(function(){
                var myButton = registry.byId("buttonNode");
                if (myButton) {
                    myButton.setLabel("Count 2");
                } else {
                    myButton = new Button({
                        label: "Count 2",
                        onClick: function(){
                            count++;
                            change();
                        }
                    }, "buttonNode");
                }
            });
        });
    }
于 2013-02-07T09:09:09.493 回答
0

我对 Dojo 1.6 比 1.7+ 更熟悉,但绝对在 1.6 中,您需要先从注册表中删除一个小部件。

因此,我认为您需要使用dijit/registry而不是dojo/dom-construct

require(["dojo/dom", "dijit/registry", "dojo/on", "dojo/domReady!"], function(dom, registry, on) {
    registry.remove(dom.byId("buttonNode"));
});
于 2013-02-07T08:28:24.040 回答