键入不匹配,必需节点,找到字符串

时间:2017-11-04 18:12:55

标签: kotlin kotlinx-html

尝试使用Kotlin点下HTML Builder网站上的代码段,因此我写了以下内容:

val tbl = createHTML().table {
    for ((num, string) in data) {
        tr {
            td { +"$num" }
            td { +string }
        }
    }
}
document.getElementById("container")!!.appendChild(tbl)

但IDE是tbl的基础,错误如下:

enter image description here

我在这做什么错误?

1 个答案:

答案 0 :(得分:0)

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)
相关问题