简体   繁体   English

在div中像在文本区域中一样包装文本

[英]Wrapping the text the same way in a div as in a textarea

I'm creating a suggestion panel while live editing an HTML textarea. 我在实时编辑HTML文本区域时创建了一个建议面板。 To get the {x,y} coordinates I use an hidden div placed behind the textarea. 为了获得{x,y}坐标,我在文本区域后面使用了一个隐藏的div。 I get the carret position and then copy the text before the carret plus a span tag to the hidden div. 我得到了carret的位置,然后将carret之前的文本加上一个span标签复制到隐藏的div中。 Then I get the span coordinates and give it to the suggestion panel. 然后,我获得了跨度坐标并将其提供给建议面板。

The only problem is that when I add a long line without spaces to the textarea, the line is being wrapped while it's cut in the div panel (so the suggestion panel is not well placed anymore until I add a carriage return). 唯一的问题是,当我在textarea中添加长行且没有空格时,该行在div面板中被剪切时被包裹了(因此,在添加回车符之前,建议面板的位置不再合适)。

Is there a way to fit the text the same way in these two places (textarea and div panel) ? 有没有办法在这两个地方(textarea和div面板)以相同的方式适合文本?

(i'm using jQuery) (我正在使用jQuery)

为DIV提供自动word-wrap:break-word的样式将强制自动换行,并使DIV像文本框一样处理长文本。

Add overflow:auto or overflow:scroll and a width: / height: to your div 's css, this will give you a scroll bar that will wrap the text and look like a textarea, well without a border. 在您的div的CSS中添加overflow:autooverflow:scrollwidth: / height: ,这将为您提供一个滚动条,该滚动条将文本包裹起来并看起来像文本区域,并且没有边框。 You can give it a border style as well to make it appear more like a textarea. 您也可以为其设置border样式,使其看起来更像文本区域。

Like this: 像这样:

 <div style="overflow:auto;width:150px;height:50px;border:1px solid #CCC;">

    Your text goes here

 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM