简体   繁体   English

如何在网页中移动dojo脚本?

[英]How can I move a dojo script in my webpage?

I have a working JavaScript page that uses dojo to check the results of a dijit/Dialog form. 我有一个工作的JavaScript页面,该页面使用dojo检查dijit/Dialog表单的结果。

<div class="content">
    <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
<form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
    <script type="dojo/on" data-dojo-event="submit" data-dojo-args="e">
        require(["dijit/registry"],function(registry){
            e.preventDefault(); // prevent the default submit
            if(!myForm.isValid()){ alert("Please fix fields"); return; }
            var inputField = registry.byId("input-field");
            var resultVal = inputField.get("value");
            console.log("Would submit "+resultVal+" here via dojo/xhr.");
            window.alert("Would submit "+resultVal+" here via dojo/xhr.");
            myFormDialog.hide();
        });
    </script>
    <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
    </div>
    <div class="dijitDialogPaneActionBar">
            <button data-dojo-type="dijit/form/Button" type="submit">OK</button>
            <button data-dojo-type="dijit/form/Button" type="button"
                data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
    </div>
 </form>
</div>
<p>When pressing this button the dialog will popup:</p>
<button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
<script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
    myFormDialog.show();
</script>
</button>

</div>
<script>
  require(["dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", "dojo/on"]);';
</script>

I want to move the script in the form to the bottom of the page, so that I can integrate it with another page. 我想将脚本以表格的形式移至页面底部,以便将其与另一个页面集成。

(This code is generated by PHP, and I use a heredoc to include code from a dojotools example.) (此代码由PHP生成,我使用Heredoc包含来自dojotools示例的代码。)

I tried the following code: 我尝试了以下代码:

<div class="content">
<div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
<form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
    <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
    </div>
    <div class="dijitDialogPaneActionBar">
            <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my-button" id="my-button" data-dojo-args="e">OK</button>
            <button data-dojo-type="dijit/form/Button" type="button"
                data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
    </div>
 </form>
</div>

<p>When pressing this button the dialog will popup:</p>
<button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
  <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
      myFormDialog.show();
  </script>
</button>
</div>
<script>
require(["dojo/dom", "dojo/on", "dijit/registry", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", ],
function(dom, on, registry){
  on(dom.byId("my-button"), "click", function(e){
      e.preventDefault(); // prevent the default submit
      console.log("Reached on() function.");
  })
});
</script>

This results in the following on the page (copied from Chrome developer tools, where console does not show any syntax errors): 这将导致页面上的以下内容(从Chrome开发人员工具复制,其中控制台未显示任何语法错误):

<div class="content">
  <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none">
    <form data-dojo-type="dijit/form/Form" data-dojo-id="myForm">
      <div class="dijitDialogPaneContentArea">
        <label for='foo'>Foo: </label>
        <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/TextBox" />
      </div>
      <div class="dijitDialogPaneActionBar">
        <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my-button" id="my-button" data-dojo-args="e">OK</button>
        <button data-dojo-type="dijit/form/Button" type="button"
          data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button>
      </div>
    </form>
  </div>
  <p>When pressing this button the dialog will popup:</p>
  <button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me!
    <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt">
      myFormDialog.show();
    </script>
  </button>
</div>
<script>
require(["dojo/dom", "dojo/on", "dijit/registry", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/TextBox", ],
function(dom, on, registry){
  on(dom.byId("my-button"), "click", function(e){
      e.preventDefault(); // prevent the default submit
      console.log("Reached on() function.");
  })
});</script>

In this case, the on() handler does not appear to be called. 在这种情况下,似乎没有调用on()处理函数。 The Show Me button appears. 显示我按钮。

If you click it, it shows the form with the Foo field. 如果单击它,它将显示带有Foo字段的表单。 If you put a value in the field and click the OK button, the form disappears. 如果您在该字段中输入一个值,然后单击“ 确定”按钮,该表单将消失。 But no message is posted to the console. 但是没有消息发布到控制台。

Does anyone have a suggestion why the script will work as part of the form, but not as a separate call on the page? 是否有人建议为什么该脚本将作为表单的一部分而不是页面上的单独调用起作用?

The original code comes from the "Forms and Functionality in Dialogs" section of the dijit/Dialog page ( https://dojotoolkit.org/reference-guide/1.10/dijit/Dialog.html#dijit-dialog ). 原始代码来自dijit / Dialog页面( https://dojotoolkit.org/reference-guide/1.10/dijit/Dialog.html#dijit-dialog )的“对话框中的表单和功能”部分。

First the on(dom.byId("my-button"), "click", function(e){...}) will not fire because it's attached to a dom not wich is replaced after by dojo parser , 首先, on(dom.byId("my-button"), "click", function(e){...})将不会触发,因为它被附加到不属于dom的dom之后被dojo parser替换,

also you have declared the button with both data-dojo-id and id , 同样,您已经用data-dojo-idid声明了按钮

note that data-dojo-id (don't use the minus - in naming here use underscore _ instead ) create a global element with this id in window object , so later you can access your element directly like here my_button.on(...) 请注意, data-dojo-id (不要在数据库名称中使用减号- ,而应使用下划线_ )在window对象中创建具有此ID的全局元素,因此稍后您可以像在这里my_button.on(...)

while by the id it create a widget with widgetid=id in order to access it by dijit/registry . 而通过id创建带有widgetid=id的小部件,以便通过dijit/registry访问它。

Second : the important point is in order to access those widget here you should wait until they are all parsed , so using callback function dojo/ready to be sur that all widgets are correctly parsed , 第二:重要的一点是要在这里访问这些小部件,您应该等待它们全部解析完毕,因此请使用回调函数dojo / ready确保所有小部件都已正确解析,

Soo in your case after the ready (widget parsed , there is no generated dom item with id="my-button" its replaced by the widget itselef ) thus the submit button will submit directly and no preventdefault actionned there also no message 在您准备好后,您就可以了(小部件已解析,没有生成的dom项目被id="my-button"替换为小部件itselef),因此提交按钮将直接提交,并且没有preventdefault操作,也没有消息

See below working snippet : 参见下面的工作片段:

 <script type="text/javascript"> dojoConfig = { isDebug: true, async: true, parseOnLoad: true } </script> <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script> <link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet" /> <body class="claro"> <div data-dojo-type="dijit/Dialog" data-dojo-id="myFormDialog" title="Form Dialog" style="display: none"> <form data-dojo-type="dijit/form/Form" data-dojo-id="myForm" action="#"> <div class="dijitDialogPaneContentArea"> <label for='foo'>Foo: </label> <input type="text" name="input-field" id="input-field" data-dojo-id="input-field" value="Foo here" required="true" data-dojo-type="dijit/form/ValidationTextBox"/> </div> <div class="dijitDialogPaneActionBar"> <button data-dojo-type="dijit/form/Button" type="submit" data-dojo-id="my_button" id="my-button" data-dojo-args="e">OK</button> <button data-dojo-type="dijit/form/Button" type="button" data-dojo-props="onClick:function(){myFormDialog.hide();}">Cancel</button> </div> </form> </div> <p>When pressing this button the dialog will popup:</p> <button id="buttonThree" data-dojo-type="dijit/form/Button" type="button">Show me! <script type="dojo/method" data-dojo-event="onClick" data-dojo-args="evt"> myFormDialog.show(); </script> </button> <script> require(["dojo/dom", "dojo/on", "dijit/registry", "dojo/ready", "dijit/Dialog", "dijit/form/Form", "dijit/form/Button", "dijit/form/ValidationTextBox"], function(dom, on, registry, ready) { ready(function() { // my_button same as = registry.byId("my-button"); my_button.on("click", function(e) { e.preventDefault(); // prevent the default submit console.log("Reached on() function.",myForm.validate()); if(myForm.validate()) { //do som stuff here and then close dilaog } }); }); }); </script> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM