在占位符中添加 HTML [英] Adding HTML Within Placeholder

查看:59
本文介绍了在占位符中添加 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用 HTML 占位符,因为它有助于向用户描述他们需要输入的内容类型.但是,有时您需要向用户提供比简单句子更多的信息.基本上我希望能够在我的 textarea 占位符中添加换行符、制表符等.我听说过使用特殊编码来做到这一点(并且已经使用这些编写了代码片段),但我发现它相当不可靠.例如,某些浏览器会为代码显示不同数量的空白.此代码的示例如下所示:

I am a fan of using HTML placeholders, as it helps describe to the user what sort of content they need to input. However, sometimes you need to give the user more information than just a simple sentence. Basically I'd like to be able to add line breaks, tabs, etc. into my textarea placeholder. I've heard of using special coding to do this (And have written pieces of code using these), but I find it rather unreliable. For example, certain browsers will display different amounts of white space for the codes. An example of this code would be like the following:

<textarea name="parts" id="parts" placeholder="Some-Part&#x0a;&#x09;&#x09;&#x09;&#x0a;&#x09;&#x09;&#x09;&#x09;&#x0a;&#x09;&#x09;&#x09;&#x09;&#x0a;..."></textarea>

因此,我对解决方案的想法是能够将实际的 HTML 元素放入占位符中.我一直在研究如何实现这一点,但一直无法找到任何关于它的东西.您知道我可以去哪里找到与此类似或相同的功能吗?或者,除了我可以做的事情之外,您还有其他选择吗?

Thus, my thought at a solution would be to have the ability to put actual HTML elements into the placeholder. I have been researching on how I might accomplish this, but have been unable to find anything on it. Do you have any idea where I might go to find functionality similar or the same as this? Or, do you have an alternative of what I might be able to do to accomplish the same thing?

推荐答案

根据定义,placeholder 属性将纯文本作为其值.没有识别标签.字符引用被识别,但像 TAB U+0009 (&#09;) 这样的字符的效果被定义为依赖于实现.

The placeholder attribute has plain text as its value, by definition. No tags are recognized. Character references are recognized, but the effects of characters like TAB U+0009 (&#09;) are defined to be implementation-dependent.

所以不,你不能在那里有 HTML 标记.您的最终问题可能有解决方案,但您没有描述该问题.通常,如果您需要不是短纯文本字符串的 placeholder 值,您将尝试根据其定义使用该属性.它旨在提示增加字段标签和描述,而不是替换它们.通常,任何需要的描述都应该在字段之前,您可以根据需要使用 HTML 标记.

So no, you can’t have HTML markup there. Your ultimate problem might have a solution, but you have not described that problem. As a rule, if you need placeholder value that is not a short plain text string, you are trying to use the attribute against its definition. It is meant to be a hint that augments the field label and description, instead of replacing them. Normally, any descriptions that are needed should precede the field, and there you can use HTML markup as desired.

这篇关于在占位符中添加 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆