输入字段值中的 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5823835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML in the input field value
提问by weardstuff
How can I get HTML to work in the value of the input field ? If you include HTML in the value, it appears as pure text. Is there a way to do something like this?
如何让 HTML 在输入字段的值中工作?如果在值中包含 HTML,它会显示为纯文本。有没有办法做这样的事情?
<input type='text' value='<b>tekst</b>'></input>
So the the output is:
所以输出是:
tekst
测试
instead of
代替
<b>tekst</b>
<b>tekst</b>
I think that was bad example... I want every appropriate HTML tag to work. If i want to include an image, the image appears in the input, if i want to add a tag ... the text should appear as a link linked.
我认为那是个坏例子...我希望每个合适的 HTML 标签都能工作。如果我想包含一个图像,图像会出现在输入中,如果我想添加一个标签......文本应该显示为链接的链接。
回答by Dan Blows
I'm not sure from your question whether you are trying to make the value contain HTML, or whether you want to apply HTML to something inside the input.
从您的问题中,我不确定您是要使值包含 HTML,还是要将 HTML 应用于输入中的某些内容。
If you want to be able to set the value to some HTML, like <input value="<b>Some HTML</b>">
where the value will actually show the HTML, you need to encode the HTML brackets like <input value="<b>Some text<b/>">
. There are functions available to do this for you automatically in most scripting languages (e.g. htmlentities() in PHP).
如果您希望能够将值设置为某些 HTML,例如<input value="<b>Some HTML</b>">
该值将实际显示 HTML 的位置,则需要对 HTML 括号进行编码,例如<input value="<b>Some text<b/>">
. 在大多数脚本语言(例如 PHP 中的 htmlentities())中,有一些函数可以自动为您执行此操作。
If you want to apply HTML to the input value, short answer is... you can't. If you want formatting on there, you could use the contentEditable attribute in HTML5 which lets you just edit-in-place (think Flickr headers).
如果您想将 HTML 应用于输入值,简短的回答是……您不能。如果你想在那里格式化,你可以使用 HTML5 中的 contentEditable 属性,它允许你就地编辑(想想 Flickr 标题)。
If you just want a standard input field to be styled, you can just use CSS like the other answers suggested.
如果您只想设置标准输入字段的样式,则可以像建议的其他答案一样使用 CSS。
回答by Tom Claus
You can try to decode you entities from your postvalue. In PHP you can do this with html_entity_decode();
您可以尝试从您的后值中解码您的实体。在 PHP 中,您可以使用html_entity_decode();
回答by Admin
You have to encode all the HTML you want in the value and paste it in the input value.
您必须对值中所需的所有 HTML 进行编码并将其粘贴到输入值中。
If you encode this:
如果你编码这个:
"http://sansoftmax.blogspot.com/"
It will look like this:
它看起来像这样:
"http://sansoftmax.blogspot.com/"
In input:
在输入中:
value=""http://sansoftmax.blogspot.com/""
回答by GreenMatt
I don't think you can put HTML inside a text field and have it interpreted as HTML instead of as text in the field.
我不认为您可以将 HTML 放入文本字段并将其解释为 HTML 而不是字段中的文本。
To accomplish what you want you'll have to use CSS. An in-line example to bold the text as you cited in your example:
为了完成你想要的,你必须使用 CSS。一个内嵌示例,用于将您在示例中引用的文本加粗:
<input type="text" style="font-weight: bold;" value="tekst" />
回答by Mike Thomsen
Try CSS:
试试 CSS:
input[type=text] {
font-weight: bold;
}