设置文本字段内部分文本的格式

发布于 2024-09-14 02:19:23 字数 118 浏览 10 评论 0原文

假设您的网页中有一个文本字段。文本字段内的值为“这是您的欢迎文本”。有什么方法可以为“欢迎”而不是其他词提供红色。

简而言之,在文本字段内的文本的某些部分应用样式。

有人可以建议一种方法吗?

Suppose you have a text-field in your webpage. The value inside text-field is "This is your welcome text". Is there any way by which I can provide red color to "welcome" and not to other words.

In short applying styling on some part of text inside a text-field.

can some body suggest a way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身骄傲 2024-09-21 02:19:23

不带文本字段。您可以使用 div 或其他带有 contentEditable 设置的元素

<div contentEditable="true">
This is your <b style="color:red";>welcome</b> text
</div>

,但是根据您想要对该文本执行的操作,有一些注意事项。

Not with a text field. You could use a div or some other element with contentEditable set

<div contentEditable="true">
This is your <b style="color:red";>welcome</b> text
</div>

However there are caveats depending on what you want to do with that text.

怪我闹别瞎闹 2024-09-21 02:19:23

除非您使用某种富文本编辑器,例如CKEditor, TinyMCE,等等但是我再次认为不值得为这样的小任务合并一个丰富的编辑器。

Not possible unless you are using some sort of rich text editor such as CKEditor, TinyMCE, etc But again I don't think it will be worth incorporating a rich editor for such minor task.

笑脸一如从前 2024-09-21 02:19:23

除了应用于其中所有文本的 CSS 样式外,您不能在

我建议您使用现成的富文本编辑器来满足您的需求,但如果您想制作自己的富文本编辑器,那么有很多可用资源

You can't do any styling in a <textarea> except CSS styles that applies to all text inside it. What most rich text editors do is programmatically replace the textarea with an iframe with contentEditable set to true. The way they go about it to make it seamless is relatively advanced and complex, but can be done.

I would recommend just using an off-the-shelf rich text editor for your needs, but if you want to make your own there are plenty of resources available.

眼中杀气 2024-09-21 02:19:23

RoToRa 是对的,只是你需要使用 TRUE,而不是 yes。

下面的代码有效。您必须对其进行更多样式设置,使其看起来像文本框。例如,在 Firefox 中,当您单击它时,它周围会出现一个丑陋的边框。但这正是你想要的。

<div contenteditable="true" style="width:200; border:1px solid black; padding:1.5;">
This is your <b style="color:red";>welcome</b> text
</div>

尝试使用 CSS 来设置样式,而不是使用内联样式。由于您有很多文本框,这样会更好。我只是使用内联样式来完成它,让您了解如何使其看起来更像文本框。边框和轻微的填充。然后您还需要将其设置为某种可滚动的 div。但这是另一个问题。

RoToRa's right, except you need to use TRUE, not yes.

The below code works. You will have to style it more to make it look like a textbox. In Firefox, for example, when you click it, an ugly border appears around it. But this is something like what you want.

<div contenteditable="true" style="width:200; border:1px solid black; padding:1.5;">
This is your <b style="color:red";>welcome</b> text
</div>

Try using CSS to style it instead of using an inline style. Since you have lots of textboxes it would be better that way. I just did it using an inline style to give you an idea of how you can make it look more like a textbox. Border and a slight padding. And then you'll also need to make it some sort of scrollable div. But that's a different question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文