Textarea:elem.val()vs elem.text() [英] Textarea : elem.val() vs elem.text()

查看:145
本文介绍了Textarea:elem.val()vs elem.text()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪。显然,我可以使用.val()和.text()来操作textarea文本。

This is very weird. Apparently, I can use both .val() and .text() to manipulate textarea text.

但是在使用.val更改文本后,我再也不能使用.text了。相反的情况并非如此。

But after I use .val to change the text, I cannot use .text anymore. The converse is not true.

这导致了一些时髦的错误。原因是因为我使用的插件可能正在使用.val来操作文本。

This is leading to some funky bugs. The reason is because a plugin i am using might be using .val to manipulate the text.

任何人都可以解释这是如何工作的吗?谢谢!

Can anyone explain how this works? Thanks!

推荐答案

你必须使用 val(),或者(更好) textarea的属性。 text()最初起作用,因为textarea的初始值由它包含的文本节点(如果有)确定。您甚至可以最初更改此文本节点的 nodeValue data 属性,它将更新textarea的值。但是,只要用户更改了textarea或脚本的值已经更改了textarea的属性,文本节点就不在图片中了以任何方式更长时间地限制textarea的价值。

You have to use val(), or (better) the value property of the textarea. text() works initially because the initial value of the textarea is determined by the text node (if any) it contains. You can even initially change the nodeValue or data property of this text node and it will update the textarea's value. However, as soon as either the user has altered the value of the textarea or script has altered the value property of the textarea, the text node is out of the picture and is no longer bound to the textarea's value in any way.

这篇关于Textarea:elem.val()vs elem.text()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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