我正在与 Ian Li 的 Raphael SketchPad 合作,并且在保存绘图时遇到了一些问题。
在我的 sketchpad
上更改时,id 为 data
的对象应该得到更新,我放置了一个控制台日志“更改”只是为了检查。发生的情况是,在加载屏幕时我看到了控制台日志消息,而在进行更改后我再也看不到它了。
如何让 console.log 仅在更改后运行? 这是我正在使用的模板的扩展代码: https://gist.github.com/benbakhshi/5143484
<script type="text/javascript">
var sketchpad = Raphael.sketchpad("editor", {
height: 260,
width: 260,
editing: true // true is default
});
// When the sketchpad changes, update the input field.
sketchpad.change(function() {
$("#data").val(sketchpad.json());
console.log("changed");
});
</script>
最佳答案
问题看起来您正在尝试 console.log
而不保证 .json
调用确实已完成。相反,请尝试连接到 #Data
的 change
事件以执行日志记录。这将保证 .json
调用在记录之前已经完成。
类似于:
var sketchpad = Raphael.sketchpad("editor", {
height: 260,
width: 260,
editing: true // true is default
});
$("#data").change(function () {
console.log("changed");
});
// When the sketchpad changes, update the input field.
sketchpad.change(function() {
$("#data").val(sketchpad.json()).change();
});
关于Javascript console.log 过早加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364160/