简体   繁体   English

Textarea在浏览器之间更新的方式不同

[英]Textarea not updating the same way between browsers

I have a textarea that I am trying to update using jQuery and I am finding that I get different results based on what browser I am using. 我有一个尝试使用jQuery更新的textarea ,但是我发现根据所使用的浏览器,我得到了不同的结果。

Have a look at this: https://jsfiddle.net/JBookham/mwaa6z2f/4/ 看看这个: https : //jsfiddle.net/JBookham/mwaa6z2f/4/

I am using $('#txtArea').html("Example text"); 我正在使用$('#txtArea').html("Example text"); to update the text that is shown. 更新显示的文本。 This works initially, until I try and type something in the textarea. 此方法最初有效,直到我尝试在文本区域中键入内容。 After I do this in Chrome, .html() stops updating the value of the textarea. 在Chrome中执行此操作后, .html()停止更新textarea的值。 I have debugged through the JavaScript and can see that the innerHtml of the textarea is being updated, but not the value. 我已经调试了JavaScript,可以看到textarea的innerHtml正在更新,但没有更新。 I try to do the same thing in Edge and the innerHtml and the value get updated as expected. 我尝试在Edge和innerHtml中做同样的事情,并且值按预期更新。

My question is, does anyone know why this happens or knows of a way that I can get it working the same way between browsers? 我的问题是,是否有人知道为什么会发生这种情况,还是有人知道我可以使它在浏览器之间以相同方式工作?

Thanks 谢谢

Update : 更新
.val() does update the textarea the same way between browsers, but it doesn't provide the same functionality that I was looking for. .val()确实在浏览器之间以相同的方式更新了textarea,但是它没有提供我一直在寻找的相同功能。 However this answer had a way that I could use .val() and still be able to decode the text I was passing in, like so: 但是, 此答案有一种方法,我可以使用.val()并且仍然能够解码我传入的文本,如下所示:
var decoded = $("<textarea/>").html("other text &amp;").val(); $('#txtArea').val(decoded);
It feels a bit hacky, like there should be a better way, but at least for the moment it's given me something that works the same way between browsers. 感觉有些棘手,就像应该有一个更好的方法一样,但是至少就目前而言,它为我提供了在浏览器之间能以相同方式工作的功能。

You should use .val() to do this. 您应该使用.val()来执行此操作。

https://jsfiddle.net/mwaa6z2f/7/ https://jsfiddle.net/mwaa6z2f/7/

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

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