如何在 clojurescript 中创建 onclick 事件?

How to create an onclick event in clojurescript?

html 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <body>
    <h1 id="app">This is clojure</h1>
  <button "test" onclick="hi()">hello</button>
    <script src="out/main.js" type="text/javascript"></script>
  </body>
</html>

你好世界文件: (ns 你好-world.core)

(println "Hello world!")

(def hi []
(println "test")

  )

不应该在这个 clojure 脚本项目中调用“hi”吗?

没有。 CLJS 中的每个引用都是命名空间的。如果有的话,那就是 hello_world.core.hi() 而不是 hi()。然而,你不应该以这种方式连接事件。

而是将您的 HTML 更改为 <button id="test">hello</button>,然后在您的 CLJS 中的某处调用它来添加实际的事件处理程序。

(let [el (js/document.getElementById "test")]
  (.addEventListener el "click"
    (fn [e]
      (println "test")))