如何在Ktor HTML DSL中为textarea添加name属性?
在Ktor HTML DSL中为textarea添加name属性的方案
你可以通过两种方式给Ktor HTML DSL里的textarea设置name属性:
直接通过attributes设置(最直接的方式)
textarea { attributes["name"] = "comment" +"Enter text here..." }这段代码会生成你预期的HTML:
<textarea name="comment">Enter text here...</textarea>自定义扩展函数(适合频繁使用场景)
如果需要像textInput那样直接传name参数,可以自己封装一个扩展函数:// 定义扩展函数 fun FlowOrContentBuilder<*>.textarea(name: String, block: FlowOrContentBuilder<TextArea>.() -> Unit = {}) { textarea { attributes["name"] = name block() } } // 使用时和textInput一样便捷 textarea(name = "comment") { +"Enter text here..." }
原因说明:textInput是Ktor为<input type="text">专门封装的DSL函数,自带name参数;但textarea函数本身没有预设该参数,所以需要通过通用的attributes集合来手动添加HTML属性。
内容的提问来源于stack exchange,提问作者zeppal
相关产品推荐
相关产品推荐

