如何通过javascript设置没有id的textarea的值? [英] How to set a value of the textarea without id by javascript?

查看:137
本文介绍了如何通过javascript设置没有id的textarea的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我们运行javascript代码来设置任何值:

Typically we run javascript code to set any value:

document.getElementById('id_name').value = "...";

但我有一个这样的页面:

But I have a page like this:

<div id="id_name">
    <div class="class_name">
        <textarea></textarea>
    </div>
</div>

如何通过javascript设置textarea的值?

How to set a value of the textarea by javascript?

非常感谢您的帮助!

推荐答案

如果您的HTML非常简单,那么您可以这样做:

You could do this, if your HTML is really that simple:

var textarea = document.getElementById('id_name').getElementsByTagName('textarea')[0];
textarea.value = "hello world";

在较新的浏览器中还有一个getElementsByClassName()可用于查找class_name < div> 元素,您可以从中执行getElementsByTagName()调用。

There's also a "getElementsByClassName()" in newer browsers that could be used to find the "class_name" <div> element, from which you'd do the "getElementsByTagName()" call.

这篇关于如何通过javascript设置没有id的textarea的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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