javascript如何将焦点移除

javascript如何将焦点移除,第1张

javascript如何将焦点移除

javascript将焦点移除的方法:1、利用onblurs属性,语法“onblur="需要执行的js代码”;2、利用blur()函数,语法“$(selector).blur(function);”。

本教程 *** 作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript 焦点移除

1、利用onblurs属性

onblur 属性在元素失去焦点时触发。

语法

onblur="SomeJavaScriptCode"

支持该事件属性的 HTML 标签:

<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>,<button>, <caption>, <cite>, <dd>, <del>, <dfn>, <div>, <dl>, <dt>,<em>, <fieldset>, <form>, <frame>, <frameset>, <h1> to <h6>, <hr>, <i>,<iframe>, <img>, <input>, <ins>, <kbd>, <label>, <legend>, <li>,<object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>,<strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>,<th>, <thead>, <tr>, <tt>, <ul>, <var>

支持该事件的 JavaScript 对象:

button, checkbox, fileUpload, layer, frame, password,radio, reset, submit, text, textarea, window

实例:

<html>

<head>
<script type="text/javascript">
function upperCase()
{
var x=document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
</head>

<body>

Enter your name: <input type="text" id="fname" onblur="upperCase()"><br />
Enter your age: <input type="text" id="age" onblur="alert(this.id)">

</body>
</html>

失去焦点时,d出一个d窗:

2、利用blur() 方法

当元素失去焦点时发生 blur 事件。

blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码。

语法:

$(selector).blur(function)

示例

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("input").focus(function(){
    $("input").css("background-color","#FFFFCC");
  });
  $("input").blur(function(){
    $("input").css("background-color","#D6D6FF");
  });
});
</script>
</head>
<body>
Enter your name: <input type="text" />
<p>请在上面的输入域中点击,使其获得焦点,然后在输入域外面点击,使其失去焦点。</p>
</body>
</html>

【推荐学习:javascript高级教程】

以上就是javascript如何将焦点移除的详细内容,

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

原文地址: https://outofmemory.cn/web/690998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-21
下一篇 2022-04-21

发表评论

登录后才能评论

评论列表(0条)

保存