gpt4 book ai didi

javascript - 如何使占位符不会在焦点上消失

转载 作者:太空狗 更新时间:2023-10-29 12:35:32 24 4
gpt4 key购买 nike

我试图在文本区域的背景下划线,所以我将占位符值设置为“__”,这将使文本区域像这样

enter image description here

<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);

最佳答案

正如其他人所说,您应该使用背景在文本下方显示永久线条。唯一的问题是背景是附加到元素上的,而不是文本,所以滚动文本不会移动背景;并且只将滚动锁定到整行是不可能的。

因此,我们必须让文本区域全高,这样背景才能始终正确。然后我们将该文本区域放入定义高度的包装元素中,并将更多内容放入溢出中。为了使文本区域自动使用必要的高度,我们不使用 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>

关于javascript - 如何使占位符不会在焦点上消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18055108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com