(注意:我也在https://github.com/gkz/LiveScript/issues/731上问过这个问题)
当我直接在 html 文件中使用 LiveScript 时,我无法在看到时立即运行 livescript 代码。例如;
...
<script src="livescript.js"></script>
<div class="my_target"></div>
<script type="text/ls">
# my livescript code will interact with div.my_target
</script>
<script>
/* a javascript code that will interact with div.my_target
</script>
<script type="text/ls">
# my livescript code does something else
</script>
<script>
var LiveScript = require("LiveScript");
LiveScript.go();
</script>
LiveScript 代码都将运行,但与之交互的 LiveScript 代码将div.my_target
在javascript 代码运行之后与其交互,而不是之前
如果我定义
<script>
var LiveScript = require("LiveScript");
LiveScript.go();
</script>
每次定义 LiveScript 代码后,所有 LiveScript 代码都会运行一次以上。
### livescript code 1
### LiveScript.go()
...
### livescript code 2
### LiveScript.go()
...
### livescript code 3
### LiveScript.go()
...
执行此代码时:
livescript code 1
将运行 3 次,首先将在定义后立即运行livescript code 2
将运行 2 次,第一次将在定义后立即运行livescript code 3
将运行 1 次,这将在定义后立即运行
如果是这样,LiveScript将更容易在 html 中使用,并且会像 Web 开发的原生语言一样使用
...
<script src="livescript.js"></script>
<script>
var LiveScript = require("LiveScript");
LiveScript.doSomeMagic_Kaboooommm();
</script>
...
<script type="text/ls">
# my livescript code
</script>
... more html
<script type="text/ls">
# my livescript code
</script>
...
<script type="text/ls">
# my livescript code
</script>
...
有没有办法做到这一点?