JavaScript/HTML 为输入类型文本区域替换行颜色?

发布于 2024-12-08 19:22:04 字数 127 浏览 0 评论 0原文

有没有一种简单的方法可以让 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 技术交流群。

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

发布评论

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

评论(3

谎言 2024-12-15 19:22:04
textarea {
  background-image: linear-gradient(#F1F1F1 50%, #F9F9F9 50%);
  background-size: 100% 4rem;
  border: 1px solid #CCC;
  width: 100%;
  height: 400px;
  line-height: 2rem;
  margin: 0 auto;
  padding: 4px 8px;
}

在 codepen 上找到了这个。为我工作。

textarea {
  background-image: linear-gradient(#F1F1F1 50%, #F9F9F9 50%);
  background-size: 100% 4rem;
  border: 1px solid #CCC;
  width: 100%;
  height: 400px;
  line-height: 2rem;
  margin: 0 auto;
  padding: 4px 8px;
}

Found this on codepen. Working for me.

阳光下的泡沫是彩色的 2024-12-15 19:22:04

如果我理解正确,您希望颜色在文本区域内交替(如每行)?

我建议最简单的方法是在文本区域中使用背景图像,并使替代颜色的行与字体大小/行高具有相同的高度,以创建替代行的错觉,然后重复背景图像。

其他解决方案

但是,似乎使用该方法,背景不会随着每行滚动。

我能想到的最好的技术是使用 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, the textarea 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 a div and set the overflow-y (vertical) to scroll and the overflow-x (horizontal) to hidden. What this does is now give us a "fake" scrollbar on our textarea, 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/

小巷里的女流氓 2024-12-15 19:22:04

CSS 现在支持 nth child 语法。查看 MDN 文档,了解仅更改无序列表中所有其他列表项的背景颜色的示例:

HTML:

<p>NBA players with most championships:</p>
<ul>
    <li>Bill Russell</li>
    <li>Sam Jones</li>
    <li>Tom Heinsohn</li>
    <li>K. C. Jones</li>
    <li>Satch Sanders</li>
    <li>John Havlicek</li>
    <li>Jim Loscutoff</li>
    <li>Frank Ramsey</li>
    <li>Robert Horry</li>
</ul>

CSS:

li:nth-child(even) {
    background-color: lightyellow;
}

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:

<p>NBA players with most championships:</p>
<ul>
    <li>Bill Russell</li>
    <li>Sam Jones</li>
    <li>Tom Heinsohn</li>
    <li>K. C. Jones</li>
    <li>Satch Sanders</li>
    <li>John Havlicek</li>
    <li>Jim Loscutoff</li>
    <li>Frank Ramsey</li>
    <li>Robert Horry</li>
</ul>

CSS:

li:nth-child(even) {
    background-color: lightyellow;
}

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

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