我试图在文本区域的背景下划线,因此我将占位符值等于' __',这将使文本区域像这样
<textarea placeholder="____________________________________________"></textarea>
我尝试了这个但是当我改变文本区域的值时线条消失了,那么我该如何修复呢?
var textarea=document.getElementById("textarea");
var value="";
for(var i=textarea.getAttribute("rows"); i > 1;i--)
for(var j=textarea.getAttribute("cols"); j >=0 ;j--)
value+="_";
$('#textarea').attr('placeholder',value);
答案 0 :(得分:4)
正如其他人所说,您应该使用背景在文本下方显示永久行。唯一的问题是背景附加到元素而不是文本,因此滚动文本不会移动背景;并且实际上不可能仅将滚动锁定为实线。
因此,我们必须使文本区域达到全高,因此背景总是正确的。然后我们将textarea放在一个包装元素中,该元素定义高度并将更多内容放在溢出中。为了使文本区域自动使用必要的高度,我们不使用textarea元素,而是使用contenteditable div。
背景来自CSS3线性渐变,其设计适合内部文本的行高:
.textbox {
border: 1px solid #CCC;
margin: auto;
width: 600px;
height: 100px;
overflow-y: auto;
}
.textbox div[contenteditable] {
font: 16px/18px monospace;
padding: 0px 10px;
min-height: 100%;
/* line-height - 1px for 1px high lines */
background-image: linear-gradient(to bottom, transparent 17px, #CCC 0px);
/* height is equal to line-height */
background-size: 100% 18px;
}
<div class="textbox">
<div contenteditable>
Text here.<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget
dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet
nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae,
justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula
porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in,
viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur
ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus
eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed
ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas
nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus.
Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed
fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo
eget bibendum sodales, augue velit cursus nunc.
</div>
</div>
答案 1 :(得分:1)
这里有一个解决方案:How to scroll a background image together with text in textarea?
两个选项......
background-image
必须是单行(重复)contenteditable div
。致信@ana的原始答案。