假设我有一个表单,其中有一个需要添加的文本字段。字段重复 100 次。
...
<textarea class="typeInStupid" data-text-type="execution-notes"
placeholder="Notes go here..." rows="6" id="123"></textarea>
...
...
<textarea class="typeInStupid" data-text-type="execution-notes"
placeholder="Notes go here..." rows="6" id="124"></textarea>
...
任务:使用浏览器控制台和 JavaScript 将相同的文本 block (例如“BlaBla”)添加到所有文本字段,以便:
...
<textarea class="typeInStupid" data-text-type="execution-notes"
placeholder="Notes go here..." rows="6" id="123">BlaBla</textarea>
...
...
<textarea class="typeInStupid" data-text-type="execution-notes"
placeholder="Notes go here..." rows="6" id="124">BlaBla</textarea>
...
替代有效的解决方案 - 是的,请。 :) 先感谢您。
最佳答案
解决方案是找到所有 textarea 字段,然后迭代每个字段并修改其值。
var textFields = document.querySelectorAll(".typeInStupid");
for (var i = 0; i < textFields.length; i++) {
textFields[i].value = "BlaBla";
}
请参阅JSFiddle with DOM 。您只需将 JavaScript 代码复制到浏览器控制台窗口并执行即可。
或者,如果您使用 jquery,
$(".typeInStupid").each(function(){
$(this).val("BlaBla");
});
关于javascript - 从控制台使用 JavaScript 填写表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392573/