注意:由于某些愚蠢的原因,我假设jQuery(请注意,您不需要jQuery,但会让它更容易一些).
这是一个解决方案使用文本缩进和绝对定位< span>的组合. (对于前缀部分).
$('textarea.withprefix').each(function() {
var prefix = $('')
.text($(this).data('prefix'))
.addClass('prefix')
.appendTo('body')
.css({
left: $(this).position().left + 'px',
top: $(this).position().top + 'px',
});
$(this).css({textIndent: prefix.outerWidth() + 'px'});
});
textarea, span.prefix {
font-family: arial; /* change these as needed, but ensure that the textarea and the span use the same font */
font-size: 1em;
font-weight: normal;
padding: 2px;
border-width: 1px;
}
span.prefix {
position:absolute;
}
本文介绍了一种使用jQuery结合文本缩进和绝对定位来实现特定样式效果的方法。通过遍历带有特定类的<textarea>元素,并为每个元素创建一个<span>标签作为前缀,调整其位置和宽度以匹配<textarea>的样式。
759

被折叠的 条评论
为什么被折叠?



