javascript - 获取没有 ID 的文本区域的值 - Vanilla JS
问题描述
我正在尝试获取没有 id 的 html 的值。
代码 -
<section class="post">
<textarea rows="5" placeholder="(New Post)"></textarea>
</section>
JS 我试过::
var entryText = document.textarea.value;
var entryText = document.getElementsByTagName.value;
解决方案
document.querySelector()
您可以为此任务使用更现代的方法来选择textarea
位于section
具有类的块中的第一个post
:
var entryText = document.querySelector("section.post textarea").value;
console.log(entryText);
<section class="post">
<textarea rows="5" placeholder="(New Post)">This is some default text, to show that the call to console.log() works.</textarea>
</section>
您可能希望进一步深入了解CSS 选择器的文档,这是document.querySelector()
's 参数所需要的。
推荐阅读
- mips - 在 MIPS 中,命令行参数究竟是如何存储在 $a1 中的?
- sql-server - 在golang中将datetimeoffset转换为datetime?
- java - 如何通过按下按钮来替换 div 的内容
- vb.net - 异步搜索 Outlook 全局地址列表
- node.js - Docker Express Node.js 应用程序容器未与 MongoDB 容器连接,给出错误 TransientTransactionError
- css - 使用 Flex-box 堆叠列表
- python - 从 Numpy 数组中删除单引号
- linux - Raspberry Pi 3 - 无法更改分辨率
- dart - FocusScope 被 Navigator 搞砸了
- php - 在 SQL 表中插入当前日期