![](/img/trans.png)
[英]Copy text from a text field and insert into another textarea at the current cursor position
[英]Dom insert the cursor into data text field
我是HTML DOM的初學者,我想要實現的是聚焦textarea
以便將光標放在其中以進行鍵入。
<div class="_4bl9">
<div class=" _30z _4h96">
<textarea name="xhpc_message_text" placeholder="Write something..." class="_4h98"></textarea>
<input value="" name="xhpc_message" type="hidden">
</div>
</div>
我以前使用過此代碼,但現在無法正常工作。
document.getElementsByName('xhpc_message')[0].focus()
有人可以建議我其他方法嗎? 謝謝。
我假設您要將光標放置在textarea
而不是input
字段中,因為input
字段是隱藏的。
如果這是您要執行的操作,則用於在先前代碼中選擇元素的名稱不正確,應為xhpc_message_text
。
以下示例將光標放置在textarea
:
document.getElementsByName('xhpc_message_text')[0].focus();
document.getElementsByName('xhpc_message_text')[0].select();
document.getElementsByName('xhpc_message_text')[0].focus()
<div class="_4bl9"> <div class=" _30z _4h96"> <textarea name="xhpc_message_text" placeholder="Write something..." class="_4h98"></textarea> <input value="" name="xhpc_message" type="hidden"> </div> </div>
您的腳本中有一個錯字。 不是xhpc_message
,而是xhpc_message_text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.