在google chrome扩展中使用javascript聚焦文本框

Focusing a textbox using javascript in a google-chrome extension

本文关键字:javascript 聚焦 文本 google chrome 扩展      更新时间:2023-09-26

如何在google-chrome扩展中聚焦文本框?我试过这个javascript:

<script type="text/javascript">
function setFocus()
{
     document.getElementById("Target").focus();
}
</script>
</head>
<body onload="setFocus()">
<div style="float:left">
<table cellpadding="3" cellspacing="0" id="mytable" style="float:left;">
<tbody><tr><td>Target:</td> <td><input type="text" name="Target" size="25" value="" /></td></tr>
</tbody></table>

我在多个代码论坛网站上发现了这个代码,所以我不确定javascript是否不起作用,或者如果chrome阻止它运行。

您使用的是getElementById(),但在您的示例中,Targetname属性,而不是ID

添加id="Target",如下:

<input type="text" id="Target" name="Target" size="25" value="" />

你给出的代码不起作用的原因是因为你的输入没有"target"的ID,它只有一个名称。添加正确的ID,它将工作

当您没有元素时,您希望通过ID获取元素。必须将id="Target"添加到输入中。