得到一个没有属性的错误“;占位符”;在xhtml strict 1.0中

getting an error there is no attribute "placeholder" in xhtml strict 1.0

本文关键字:占位符 xhtml strict 错误 一个 属性      更新时间:2023-09-26

我意识到占位符是html 5。占位符有替代方案吗?

我必须使用xhtml strict 1.0,这是一个要求。

你是对的,占位符是HTML5的新手,所以它在XHTML中不起作用,也没有真正的等价物。我唯一能建议的是将"值"设置为占位符文本,但用户必须在输入自己的文本之前删除文本。

唯一的其他选择是使用javascript以某种方式模拟占位符文本。

使用这个库非常容易。

只需将库包含在页面的<head>标签中(您也可以下载该文件):

<script type='text/javascript' src="https://cdn.rawgit.com/jamesallardice/Placeholders.js/master/dist/placeholders.min.js"></script>

并像通常那样使用占位符:

<input type="text" placeholder="I have a placeholder!"/>

JS Fiddle演示

xhtml确实支持占位符,因为xhtml是XML&HTML。该函数有效,但无法验证,因为验证方法还没有跟上新的编码原则。将其作为一种可验证的替代方案;

<input type="text" onfocus="if (this.value==this.defaultValue) this.value = ''" onblur="if (this.value=='') this.value = this.defaultValue" value="your placeholder text here" />