类型不匹配,所需的节点,找到的字符串
尝试在HTML Builder
轻Kotlin
使用Kotlin
网站上的代码片段,所以我写了下面的代码:
val tbl = createHTML().table { for ((num, string) in data) { tr { td { +"$num" } td { +string } } } } document.getElementById("container")!!.appendChild(tbl)
但IDE是底层的错误,如下所示:
我在这里做什么错误?
createHtml()
产生一个String,不能传给appendChild()
。 你应该改用
val tbl = document.create.table { ... }
它产生一个HTMLElement(它是一个Node)或者简单地跳过这个变量。
document.getElementById("container")!!.append.table { ... }
createHTML().xxx
最适合与服务器Ktor.io
一起使用,创建如下所示的内容:
val html = createHTML().html { body { form(action = "/login", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) { p { +"user:" textInput(name = "user") { value = principal?.name ?: "" } } p { +"password:" passwordInput(name = "pass") } p { submitInput() { value = "Login" } } } } }
然后使用以下命令将其发送到浏览器
call.respondText(html, ContentType.Text.Html)