简体   繁体   English

将不同的CSS用于空格和文本添加到文本类型输入

[英]Adding different CSS for space and text to an text type input

I have an text type input in an html form as bellow: 我有一个以html形式输入的text类型,如下所示:

<input type="text" id="tag" name="tag">

Now, I want to add style on that input text based on user's input. 现在,我想根据用户输入在该输入文本上添加样式。 This style need to be different for text and space . 对于textspace此样式需要有所不同。

For example: if someone enters php java python in the input field then I want to style php , java and python with specific background where their internal spaces will have different background. 例如:如果有人在输入字段中输入php java python ,那么我想用特定背景设置phpjavapython样式,其internal spaces将具有不同的背景。

Is it possible? 可能吗? If possible then how? 如果可能的话怎么办?

The answer is yes and no . 答案是肯定的 Yes it is possible, but it is not possible with input only. 是的,有可能,但仅输入是不可能的。

You cannot put elements inside input tags, but You could have some kind of container which would handle this part for You. 您不能将元素放入输入标签中,但是可以有某种容器来为您处理此部分。

I've attached very simple and naive example, it could be done way better, but it shows You the concept. 我已经附上了一个非常简单且幼稚的示例,可以做得更好,但是它向您展示了这个概念。

Text inside input is being split by space, and new element for each word and space is created. 输入中的文本按空格分割,并为每个单词和空格创建新元素。

You could do something like that, but with styled input inside container. 您可以执行类似的操作,但是要在容器中添加样式输入。 Then You should listen for input change. 然后,您应该收听输入更改。 When user press space, You should get the word which user wrote, create new element for word (and also one more for space), append it to container, and clear the input, so it would be ready for next, new word. 当用户按下空格键时,您应该得到用户编写的单词,为单词创建一个新元素(还有一个空格),将其附加到容器中,然后清除输入,这样就可以准备下一个新单词了。

 const textContainer = document.querySelector('#textContainer') document.querySelector('#myInput').addEventListener('input', ({ target }) => textToElements(target.value)) const textToElements = (text) => { const words = text.split(' ') const wordsElements = words.map(createWordElement) clearWordsContainer() wordsElements.forEach(putWordElement) } const createWordElement = (word) => { const element = document.createElement('span') element.classList.add('word') element.textContent = word return element } const putWordElement = (element) => { const spaceElement = document.createElement('span') spaceElement.classList.add('space') spaceElement.textContent = ' ' textContainer.appendChild(element) textContainer.appendChild(spaceElement) } const clearWordsContainer = () => textContainer.innerHTML = '' 
 .word { color: #FFF; background: #333; } .space { background: #999; } 
 <div class="container"> <div id="textContainer"></div> <input id="myInput" type="text" /> </div> 

You can't give style to text inside an input directly. 您不能直接为输入中的文本赋予样式。

I think what you are looking for is an UI element commonly named "tags input" or "chips input" or "pills input" 我认为您正在寻找的是通常称为“标签输入”或“芯片输入”或“药丸输入”的UI元素

You might use some javascript or better ("better" being a personal appreciation, see comments below) a js library to input tags/pills like this one 您可能会使用一些javascript或更好的javascript库(“ better”是个人欣赏,请参见下面的评论)一个js库来输入这样的标签/药丸

https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/ (see title Categorizing tags, which uses different colors for each tag) https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/ (请参阅标题分类标签,每个标签使用不同的颜色)

You can google for others using those names I gave you. 您可以使用我给您提供的那些名称搜索其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM