HTML textarea:使用JavaScript获取包装文本? [英] HTML textarea: use JavaScript to get wrapped text?
问题描述
如果我有这样的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 我可以提出的唯一解决方案是创建一个带有名称的隐藏iframe,并将该表单提交给该框架。然后,iframe可以通过 当然如果你愿意的话可以通过脚本调用提交。 以下我有穿透因为它不适用于这种情况。 这是做什么的 创建一个新分区(在内存中) 克隆所需元素 #foo 并将其附加到我们创建的div中 提取内部html(但是因为我们正在处理我们包裹在元素中的div,所以我们得到了元素html .. 编辑] If I've got a textarea like this: Is there any way, using JavaScript, to grab the hard-wrapped text? I've tried the obvious For example, if the textarea is: The browser will wrap the text to: And I was some way to grab that text – 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 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..
what this does is Create a new div (in memory) clone the desired element #foo and append it to the div we created extract the inner html (but since we are dealing with the div that we wrapped around our element, we get the elements html .. [EDIT] 这篇关于HTML textarea:使用JavaScript获取包装文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! window.parent
与其父代进行通信,并提供返回的文本(其中包含新窗口,正如通常提交的窗体一样。) p>
通常的做法是:
alert ($('< div />')。append($('#foo')。clone())。html());
$('< div />')
.append($('#foo')。clone())
.html()
显然我误解了..要得到一个元素的内部文本使用.text()方法
所以
$('#foo')。text();
<textarea id="foo" cols=80 wrap="hard"></textarea>
$("#foo").val()
, but that returns the unwrapped text.<textarea id="bar" cols=5 wrap="hard">a b c d e f</textarea>
a b c
d e f
"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…
).window.parent
and provide the returned text (which will include the new lines as the form has been normally submitted..)a generic way to do this would bealert( $('<div/>').append($('#foo').clone()).html() );
$('<div/>')
.append( $('#foo').clone() )
.html()
obviously i misunderstood .. to get the inside text of an element use the .text() method
so$('#foo').text();