javascript:获取textarea,textContent与innerHTML与innerText的内容 [英] javascript: get contents of textarea, textContent vs. innerHTML vs. innerText

查看:56
本文介绍了javascript:获取textarea,textContent与innerHTML与innerText的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用js获取文本区域的内容时遇到了麻烦.我觉得我之前已经做过很多次了,但都没有问题,但是有些事情使它无法完成,或者我有精神障碍.

I am having trouble getting the contents of a textarea with js. I feel like I've done this many times before without problems but something is throwing it off or I have a mental block.

html

<textarea id="productdescript">test copy..asdfd</textarea><button value="Enter" onclick="addProduct()">

js

function addProduct() {
var descript = document.getElementById('productdescript').textContent;
alert(descript);
}

Firefox是我目前拥有的唯一浏览器.

Firefox is the only browser I have currently.

当我使用textContent时,会出现警报框,但它是空白的. 当我使用值时,将出现警报框,并显示未定义" 当我使用innerHTML时,所有的HTML都会出现,包括标签.

When I use textContent, the alert box appears but it is blank. When I use value, the alert box appears and says "Undefined" When I use innerHTML, all the HTML appears including the tags.

此外,我了解到textContent仅在FF中运行,并且为了实现跨浏览器兼容性,您需要执行诸如innerText和textContent之类的操作,但textContent在FF中不起作用.此应用上没有jquery

Also, I understand that textContent only runs in FF and for cross browser compatibility you need to do something like innerText and textContent but textContent is not working in FF. There is no jquery on this app

获取textarea内容的正确的跨浏览器方式是什么!感谢您的任何建议.

What is the correct cross browser way to get contents of textarea! Thanks for any suggestions.

推荐答案

对于textarea,您只能在您的方案中使用.value(我测试了给定的代码,它可以正常工作).

For textarea, you could only use .value in your scenario (I tested your given code and it works fine). .

也是

1)请记住,在代码中提到您的元素后,才调用此函数addProduct() ,否则将为undefined.

1) keep in mind that you call this function addProduct() ONLY after your element is mentioned in the code, otherwise it will be undefined.

2)必须不要id as productdescript

3)不得存在称为productdescript

这篇关于javascript:获取textarea,textContent与innerHTML与innerText的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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