HTML textarea:使用JavaScript获取包装文本? [英] HTML textarea: use JavaScript to get wrapped text?

查看:89
本文介绍了HTML textarea:使用JavaScript获取包装文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的textarea:

 < textarea id =foocols = 80 wrap = 硬 >< / textarea的> 

有什么方法可以使用JavaScript来获取硬包装文本?



我试过了明显的 $(#foo).val(),但是它返回了解包的文本。 p>

例如,如果textarea是:

 < textarea id = barcols = 5 wrap =hard> abcde f< / textarea> 

浏览器会将文字包装到:

  abc 
def

抓住该文本 - abc \ ef(其中,因为 wrap = hard )是文本如果在< form ... )中提交< textarea ... ,那么将发送该消息。

解决方案

2 window.parent 与其父代进行通信,并提供返回的文本(其中包含新窗口,正如通常提交的窗体一样。) p>

当然如果你愿意的话可以通过脚本调用提交。

以下我有穿透因为它不适用于这种情况。
通常的做法是:

  alert ($('< div />')。append($('#foo')。clone())。html()); 

这是做什么的

创建一个新分区(在内存中)

  $('< div />')

克隆所需元素 #foo 并将其附加到我们创建的div中

  .append($('#foo')。clone())

提取内部html(但是因为我们正在处理我们包裹在元素中的div,所以我们得到了元素html ..

  .html()

编辑]

显然我误解了..要得到一个元素的内部文本使用.text()方法
所以

  $('#foo')。text(); 

If I've got a textarea like this:

<textarea id="foo" cols=80 wrap="hard"></textarea>

Is there any way, using JavaScript, to grab the hard-wrapped text?

I've tried the obvious $("#foo").val(), but that returns the unwrapped text.

For example, if the textarea is:

<textarea id="bar" cols=5 wrap="hard">a b c d e f</textarea>

The browser will wrap the text to:

a b c
d e f

And I was some way to grab that text – "a b c\nd e f" (which, because of wrap=hard, is the text that would be sent if the <textarea… was submitted in a <form…).

解决方案

Edit 2

The only solution that i can propose is to create a hidden iframe with a name, and target the form submit to that frame. Then the iframe can communicate with its parent throught the window.parent and provide the returned text (which will include the new lines as the form has been normally submitted..)

of course the submit could be called by a script if you wished ..

the following i have strike-through since it does not apply to this case.. a generic way to do this would be

alert( $('<div/>').append($('#foo').clone()).html() );

what this does is

Create a new div (in memory)

$('<div/>')

clone the desired element #foo and append it to the div we created

.append( $('#foo').clone() )

extract the inner html (but since we are dealing with the div that we wrapped around our element, we get the elements html ..

.html()

[EDIT]
obviously i misunderstood .. to get the inside text of an element use the .text() method so

$('#foo').text();

这篇关于HTML textarea:使用JavaScript获取包装文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆