【问题标题】:Type mismatch, required node, found string类型不匹配,需要的节点,找到的字符串
【发布时间】:2017-11-04 18:12:55
【问题描述】:

尝试在HTML Builder点击下的Kotlin站点使用sn-p,所以我写了以下内容:

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

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

我在这里犯了什么错误?

【问题讨论】:

    标签: kotlin kotlinx-html


    【解决方案1】:

    createHtml() 生成一个不能传递给appendChild() 的字符串。你应该改用

    val tbl = document.create.table {
        ...
    }
    

    产生一个 HTMLElement(它是一个节点)或简单地跳过变量。

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2014-09-26
      • 2019-04-30
      • 2019-08-22
      相关资源
      最近更新 更多