简体   繁体   English

如何在 Kotlin/JavaScript 中制作按钮单击侦听器事件?

[英]How to make button click listener event in Kotlin/JavaScript?

Inside IntellJ IDEA, I ve created a button in my HTML file with an id.在 IntellJ IDEA 中,我在 HTML 文件中创建了一个带有 id 的按钮。 What I'm trying to achieve is to change the header tag to "button clicked" using kotlin.我想要实现的是使用 kotlin 将标题标签更改为“单击按钮”。

Upon searching the kolinlang.org website and others resources I have trouble finding simple reference for doing specific things I wonder if there a translated kotlin/javascript site where all of them is put together like this site for example: https://www.w3schools.com/js/default.asp在搜索 kolinlang.org 网站和其他资源时,我很难找到做特定事情的简单参考,我想知道是否有一个翻译的 kotlin/javascript 站点,其中所有这些站点都像这个站点一样放在一起,例如: https://www.w3schools .com/js/default.asp

Thanks谢谢

  1. Create a Kotlin/JS project in IntelliJ, named "JSProject"在 IntelliJ 中创建一个 Kotlin/JS 项目,命名为“JSProject”
  2. Create an index.html file including a button with ID "mybutton"创建一个index.html文件,其中包含一个 ID 为“mybutton”的按钮
  3. Create a Kotlin file main.kt with the following content:使用以下内容创建 Kotlin 文件main.kt
import org.w3c.dom.HTMLButtonElement
import kotlin.browser.document

fun main(args: Array<String>) {
    val button = document.getElementById("mybutton") as HTMLButtonElement
    button.addEventListener("click", {
        document.title = "button was clicked"
    })
}
  1. Import the Kotlin JS library and your code (JS compiled from Kotlin) in at the end of your HTML file:在 HTML 文件的末尾导入 Kotlin JS 库和您的代码(从 Kotlin 编译的 JS):
       ...
       <script src="out/production/JSProject/lib/kotlin.js"></script>
       <script src="out/production/JSProject/JSProject.js"></script>
    </body>
    </html>
  1. Compile your Kotlin code to JS (menu: Build | Rebuild Project)将您的 Kotlin 代码编译为 JS(菜单:Build | Rebuild Project)

  2. Open the index.html file in a web browser and click on the button.在 Web 浏览器中打开index.html文件并单击按钮。 "button was clicked" appears in the title. “按钮被点击”出现在标题中。

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

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