JavaScript/HTML 为输入类型文本区域替换行颜色?
有没有一种简单的方法可以让 HTML
我不介意解决方案是纯 CSS 还是需要 JavaScript。
Is there an easy way to have an HTML <textarea>
alternate its row colors
to improve editing?
I don't mind if the solution is pure CSS or if it requires JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 codepen 上找到了这个。为我工作。
Found this on codepen. Working for me.
如果我理解正确,您希望颜色在文本区域内交替(如每行)?
我建议最简单的方法是在文本区域中使用背景图像,并使替代颜色的行与字体大小/行高具有相同的高度,以创建替代行的错觉,然后重复背景图像。
其他解决方案
但是,似乎使用该方法,背景不会随着每行滚动。
我能想到的最好的技术是使用 James Padolsey 的名为“autoResize”的 jQuery 插件。其作用是删除滚动条,并且当文本接近
textarea
底部时,textarea
高度会相应增加。现在,这可能会导致问题,因为您可能会拥有非常长的文本区域,具体取决于用户写入的文本量,但我已经为此创建了一个修复程序。
我们可以做的是将
textarea
包装在div
中,并将overflow-y
(垂直)设置为scroll
以及overflow-x
(水平)到hidden
。现在,它的作用是在我们的textarea
上提供一个“假”滚动条,创造出它是可滚动的错觉,因此我们的背景现在看起来就像它也与文本一起上下滚动一样。您必须相应地调整宽度/高度/边距/边框/填充等,并可能检查跨浏览器兼容性,但这应该有助于您走上正确的轨道并让您继续前进。
以下是我使用上述方法创建的示例的链接:
http://jsfiddle.net/HelloJoe/DmPLH /
If I understand correctly that you want the colors alternating WITHIN the textarea (as in each line)?
I would suggest the easiest method is to use a background image in your textarea's and have the rows of the alternate colors the same height as the font-size/line-height to create the illusion of alternate rows, then just repeat the background image.
Additional Solution
However, it seems that using that method, the background doesn't scroll along with each line.
The best technique I can come up with is to use a jQuery plugin called 'autoResize' by James Padolsey. What this does is removes the scrollbars and as your text nears the bottom of the
textarea
, thetextarea
height is increased accordingly.Now, that can cause problems since you could potentially have VERY long textareas depending on how much text the user writes but I've created a fix for this.
What we can do is wrap the
textarea
in adiv
and set theoverflow-y
(vertical) toscroll
and theoverflow-x
(horizontal) tohidden
. What this does is now give us a "fake" scrollbar on ourtextarea
, creating the illusion that it's scrollable so our background now appears as if it scrolls up and down with the text too.You will have to adjust the width/height/margins/borders/paddings etc accordingly and maybe check for cross browser compatibility, but this should help set you on the right track and get you going.
Here is a link to an example I have created using the above method:
http://jsfiddle.net/HelloJoe/DmPLH/
CSS 现在支持
nth child
语法。查看 MDN 文档,了解仅更改无序列表中所有其他列表项的背景颜色的示例:HTML:
CSS:
RESULT:
使用 CSS 的 nth-child 语法使文本区域中的每隔一行具有不同颜色的示例
SOURCE:
https://developer.mozilla.org/en-US/docs /Web/CSS/:nth-child
CSS supports an
nth child
syntax now. Check out the MDN docs for an example of changing the background-color of only every other list item inside an unordered list:HTML:
CSS:
RESULT:
An example of making every other line in a textarea a different color by using CSS' nth-child syntax
SOURCE:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child