简体   繁体   English

Dom将光标插入数据文本字段

[英]Dom insert the cursor into data text field

I'm a beginner to HTML DOM and what I want to achieve is focus textarea so that cursor will be placed in it to type. 我是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>

I was using this code before and it was working but not anymore. 我以前使用过此代码,但现在无法正常工作。

document.getElementsByName('xhpc_message')[0].focus()

Can any one suggest me other way to do? 有人可以建议我其他方法吗? Thanks. 谢谢。

I assume you want to place the cursor in the textarea , instead of the input field, because the input field is hidden. 我假设您要将光标放置在textarea而不是input字段中,因为input字段是隐藏的。

If that's what you want to do, the name you use to select the element in your previous code isn't correct, it should be xhpc_message_text . 如果这是您要执行的操作,则用于在先前代码中选择元素的名称不正确,应为xhpc_message_text

The following example places the cursor in the textarea : 以下示例将光标放置在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> 

There is a typo in your script. 您的脚本中有一个错字。 It's not xhpc_message it is xhpc_message_text 不是xhpc_message ,而是xhpc_message_text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM