我用" lua.vm.js"在网络客户端与lua一起发展。
我想知道如何从js脚本调用Lua函数。
var element = document.getElementById("myBtn")
element.addEventListener("click", function(){ /*call here Lua function*/ });
答案 0 :(得分:2)
方法#1
从Lua代码中操作JavaScript对象:
<script src="lua.vm.js"></script>
<script type="text/lua">
function YourLuaFunction()
-- your Lua code is here
end
</script>
<button id="MyBtn">Lua inside</button>
<script type="text/lua">
js.global.document:getElementById("MyBtn"):addEventListener("click", YourLuaFunction);
</script>
方法#2
使用L.execute(code)
从JS执行Lua代码:
简短的例子:
element.addEventListener("click", function(){ L.execute('YourLuaFunction()'); });
很长的例子:
<script src="lua.vm.js"></script>
<script>
function executeLua(code) {
try { L.execute(code); } catch(e) { alert(e.toString()); }
}
</script>
<script type="text/lua">
function YourLuaFunction()
-- your Lua code is here
end
</script>
<button onclick="executeLua('YourLuaFunction()')">Exec Lua code</button>