我有一个 textarea ,我在其中使用 javascript 检查上面是否写了一些东西:
if (!editorInstance.document.getBody().getChild(0).getText()) {
do some action
}
它适用于 firefox,但在 IE9 中我收到此错误,告诉它是 null 或未定义的对象(因此 IE 不检查我的条件)。所以我尝试了:
var hasText = editorInstance.document.getBody().getChild(0).getText();
if (typeof hasText === 'undefined') {
do some action
}
问题是它仍然停在第一行(var hasText = edit...
),因为editorInstance.document.getBody().getChild(0).getText()
返回 null 或 undefined
编辑
当我这样做时editorInstance.document.getBody().getChild(0).getText()
,我在 textarea 中输入了所有文本,但是当没有输入文本时(我检查它以验证此字段),此代码不返回任何内容,这就是hasText
变量没有按我预期的方式工作的原因。
关于如何解决它的任何想法?