简体   繁体   English

当使用javascript单击任意按钮时如何设置标签焦点

[英]how to set focus for label when click the any button using javascript

I have one asp:Textbox and one asp:label control in my aspx page. 我的aspx页面中有一个asp:Textbox和一个asp:label控件。 I want to set focus for label when clicking the button using javascript. 我想在使用javascript单击按钮时为标签设置焦点。 Can anyone tell me how to do this? 谁能告诉我该怎么做?

Many thanks... 非常感谢...

You need to access the element via an id or a class an call focus() : 您需要通过一个ID或一个调用focus()的类来访问该元素:

document.getElementById('myTextBox').focus();    

As Pointy pointed out: When you mean by clicking on a label to select the textbox, you just need to set the for attribute on the label to the ID of the textbox. 正如Pointy 指出的那样:当您通过单击标签以选择文本框时,只需将标签上的for属性设置文本框的ID。

<label for="myTextBox">Label Text</label>

The for attribute does it for you out of the box for属性为您提供了开箱即用的功能

<label for="{the id you want}"> </label>

MDN MDN

Because it's asp.net application you need to use the ClientID : 因为它是asp.net应用程序,所以您需要使用ClientID

<label for='<%= myTextBox.ClientID %>'> </label>
use `.focus()` it will do the focus part

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

相关问题 在 angular 2 中单击按钮时如何将焦点设置到 div? - How to set focus to the div when button click in angular 2? 按下“Enter”按钮时如何将焦点设置在下一个输入上? (javascript) - How to set focus on next input when the button "Enter" is pressed? (javascript) 使用JavaScript单击按钮后,Textarea无法聚焦 - Textarea not focus after click on button using javascript 使用按钮和 JavaScript 将焦点设置在文本输入上 - Set focus on text input using button and JavaScript 当焦点位于使用 JavaScript 的按钮上时如何检测“tab keypress” - How to detect "tab keypress" when focus is at a button using JavaScript 使用 JavaScript 在控制台中单击任何单选按钮时如何显示特定值? - How to show specific value when click on any radio button in console using JavaScript? 如何在JavaScript中使用.focus()专注于标签标记 - How can i have focus on label tag using .focus () in javascript 当我使用javascript点击它时如何获得相应的标签值 - How to get corresponding label value when i click on it using javascript 在按钮上单击可在JavaScript中输入焦点 - On button click focus input in JavaScript 单击Primefaces上的重置按钮时,如何将焦点设置在inputText上 - how can I set the focus on inputText when I click reset button on Primefaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM