简体   繁体   English

单击bonsaijs形状后显示jquery UI对话框

[英]Display jquery UI dialog after click on a bonsaijs shape

Hello I have this code 你好我有这段代码

$(function() {
  bonsai.run(document.getElementById('movie'), {
   code: function() {
     var rect= new Rect(0, 0, 100, 100);
     rect.on('multi:pointerdown', function(e) {
     $("#dialog-form").dialog("open");
     });
   },
   width: 500,
   height: 400,
  });
});

When I click on my rect I have this error : 当我单击我的矩形时,出现此错误:

ReferenceError: $ is not defined ReferenceError:未定义$

How can I get a reference on jquery ? 如何获得有关jquery的参考?

BonsaiJS creates a new execution context (often a web worker) and the code you pass within bonsai.run is executed in a different scope, where jQuery isn't available. BonsaiJS创建一个新的执行上下文(通常是Web工作者),并且您在bonsai.run传递的代码在不同的范围内执行,而jQuery不可用。 Details about how BonsaiJS executes code can be found here . 有关BonsaiJS如何执行代码的详细信息,请参见此处

But to solve your problem you can communicate with the so-called BonsaiJS runner context like that: 但是要解决您的问题,您可以像这样与所谓的BonsaiJS运行程序上下文进行通信:

$(function() {
  var movie = bonsai.run(document.getElementById('movie'), {
    // note: this function will be stringified and sent to the runner context
    code: function() {
      var rect= new Rect(0, 0, 100, 100).fill('red').addTo(stage);
      rect.on('multi:pointerdown', function(e) {
        // this is how you would pass data with your message
        stage.sendMessage('openDialog', {
          id: '#dialog-form'
        });
        // no data:
        // stage.sendMessage('openDialog', {});
      });
    },
    width: 500,
    height: 400,
  });
  movie.on('load', function() {
    movie.on('message:openDialog', function(data) {
      $(data.id).dialog("open");
    });
  });
});
  • Make sure $ variable is not over-written by Jquery variable in the core of jquery. 确保$变量不会被jquery核心中的Jquery变量覆盖。

  • The order in which the script is loaded is important, run script after jQuery loads. 脚本的加载顺序很重要,请在jQuery加载后运行脚本。

Maybe you are calling that function before the JQuery Javascript is included? 也许您在包含JQuery Javascript之前就调用了该函数? Put the references to the JQuery scripts first. 首先将对JQuery脚本的引用。

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

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