如何在没有id的textarea中输入随机值? [英] how to enter a random value in a textarea without an id?
问题描述
我必须使用油脂猴子脚本在网站的textarea
中输入一个随机值,文本区域中没有id
,所以document.getElementById
不起作用.
I have to enter a random value in a textarea
of a web site by using grease monkey script the text area didn't have an id
so document.getElementById
doesn't work.
我可以使用另一种方法吗?
Can I use another method to do that?
我当前的HTML显示我的textarea
:
My current HTML showing my textarea
:
<tr>
<td class="tdv pd5"><span class="tbg">
Message</span> </span>
</td>
<td class="pd5">
<textarea name="inpx_msg" class="msgbox"></textarea>
<br />
<div class="tsm">140 characters max.</div>
</td>
</tr>
推荐答案
You could use element.querySelector if you want to be able to specifically target an element using a CSS selector, similar to this:
var textArea = document.querySelector('table tr td.pd5 textarea.msgbox');
textArea.value = "My Random Text";
以上内容适用于以下HTML示例:
The above is working on the following HTML example:
<table>
<tbody>
<tr>
<td class="tdv pd5"><span class="tbg">Message</span> </span>
</td>
<td class="pd5">
<textarea name="inpx_msg" class="msgbox"></textarea>
<br />
<div class="tsm">140 characters max.</div>
</td>
</tr>
</tbody>
</table>
演示 -使用element.querySelector
当然,您现在可以将CSS选择器更改为您想要匹配DOM层次结构的任何内容.使用element.querySelector
有很多灵活性.
Off course, you can now change the CSS selector to anything you like to match your DOM hierarchy. There is lots of flexibility using element.querySelector
.
这篇关于如何在没有id的textarea中输入随机值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!