js中<input>如何获得焦点

js中<input>如何获得焦点,第1张

用自带的focus()就可以了

利用js中<input/>实现文本框默认获取输入焦点完整代码实现如下:

<html>

 

<head>

     

<meta >

<script type="text/javascript">       

//输入框获取鼠标焦点       

function autoFocus(){

           

var pFocus = documentgetElementById("password");

pFocusfocus();

   

pFocusselect();

   

}    

</script>

 

</head>

 

<body onload="autoFocus()">

     

<div id="loginform">

     

<h1 style="font-size:15em;padding:20px;">输入密码</h1>

     

<form action="${pageContextrequestcontextPath}/" method="post">

   

<input id="password" type="password" name="password">

 

<input type="submit" value="提交">

   

</form>

   

</div>

 

</body>

</html>

扩展资料:

JavaScript 使我们有能力创建动态页面。事件是可以被 JavaScript 侦测到的行为。 网页中的每个元素都可以产生某些可以触发JavaScript函数的事件。比方说,我们可以在用户点击某按钮时产生一个 onClick 事件来触发某个函数。事件在 HTML 页面中定义 。

参考资料:

JavaScript官方API接口-:focus

百度百科-JavaScript

W3cschool-获得字段焦点

焦点在HTML和JS中是只光标。

焦点在JS和HTML里是在页面上屏幕中闪动的小竖线,鼠标点击就可获得光标,Tab键可按照设置的Tabindex来进行切换焦点。

示例:

<divid="demo"></div>

<divid="test"></div>

<divid="one"></div>

<divid="two"></div>

<divid="three"></div>

<divid="fore"></div>

<divid="five"></div>

<divid="six"></div>

<script>

function$(id){

returndocumentgetElementById(id);

}

$("demo")stylebackgroundColor="green";

//调用方法

$("test")stylebackgroundColor="blue";

$("one")stylebackgroundColor="orange";

$("two")stylebackgroundColor="red";

$("three")stylebackgroundColor="purple";

$("fore")stylebackgroundColor="#f6e71f";

$("five")stylebackgroundColor="#5153ff";

$("six")stylebackgroundColor="#ff1496";

//调用函数,并直接修改盒子的背景颜色

扩展资料

jquery判断input输入框的值

//输入框正在输入时

$("#ipt")on('input',function(){

if(!($('#ipt')val()=='')){

$("cancle_ico")removeClass('hide');

}else{

$("cancle_ico")addClass('hide');

}

})

//输入框得到焦点时

$("#ipt")on('focus',function(){

if(!($('#ipt')val()=='')){

$("cancle_ico")removeClass('hide');

}else{

$("cancle_ico")addClass('hide');

}

})

//输入框失去焦点时

$("#ipt")on('blur',function(){

if(($('#ipt')val()=='')){

$("cancle_ico")addClass('hide');

}else{

$("cancle_ico")removeClass('hide');

}

})

用onfocus,onblur两个JS内部的一般事件,前者为获取焦点,后者为推动焦点

js代码如下:

function obr (){

var va = documentfrmuservalue

if(va == '请输入用户名'||va == ''){

documentfrmuservalue='请输入用户名';

documentfrmuserstyleborder='1px solid red';

}else{

documentfrmuserstyleborder='1px solid gray';

}

}

function ofs(){

documentfrmuservalue='';

documentfrmuserstyleborder='1px solid blue';

}

表单代码如下:

<form action='' method='' name='frm'>

<input type='text' name='user' value='请输入用户名' onfocus='ofs()' onblur='obr()'/>

</form>

以上仅供参考

以上就是关于js中<input/>如何获得焦点全部的内容,包括:js中<input/>如何获得焦点、HTML和JS中所谓的“焦点”是指什么、HTML表单验证的时候,当鼠标移开的时候给出相应的div提示信息是怎么弄的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9781937.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存