今天写了一个表单元素,在用户点击的时候会清空input中的内容,当鼠标点击其他地方的时候会把输入的值保存为input的value值

类似于这样的效果

当用户点击的时候文字消失。

html代码

     <input type="text" name="" value="请输入您的邮箱地址"/>
<input type="text" name="" value="请输入用户名"/>
<input class="pwd" type="text" name="" value="请输入密码"/>
<input class="pwd" type="text" name="" value="确认密码"/>

jq代码

<script type="text/javascript">
$(document).ready(function(e) {
var temp;
$(":text").focusin(function(){
var value = $(this).val();
if ($(this).val() == "请输入密码" || $(this).val() == "请输入您的邮箱地址" || $(this).val() == "确认密码" || $(this).val() =="请输入用户名") {
if($(this).val() == "确认密码" || $(this).val() == "请输入密码") {
$(this).attr('type','password')
}
$(this).val("")
}
//alert(value)
})
$(":input").focusout(function(event) {
/* Act on the event */
if($(this).val() == "") {
if ($(this).hasClass('pwd')) {
$(this).attr('type','text')
};
$(this).val(temp)
}
});
}) </script>

这样之后基本所要求的功能可以实现,但是发现代码不够优雅,于是又想到了可以使用数组来保存value值,

var arr_ = [];
var temp;
$(":text").each(function() {
arr_.push($(this).val())
})
$(":text").focusin(function(){
var that = this;
var value = $(that).val();
temp = value;
$.each(arr_,function(i,n) {
if(value==n){
$(that).val("");
if(value=="请输入密码"||value=="确认密码"){
$(that).attr("type","password");
}
}
});
})

又发现了一个问题, 总是需要一个全局变量temp来保存value值,这对于javascript来说是不好的,于是乎又想到了data属性

<input type="text" name="" data="请输入您的邮箱地址" value="请输入您的邮箱地址"/>
<input type="text" name="" data="请输入用户名" value="请输入用户名"/>
<input class="pwd" type="text" data="请输入密码" name="" value="请输入密码"/>
<input class="pwd" type="text" data="确认密码" name="" value="确认密码"/>
$(document).ready(function(e) {
var arr_ = [];
$(":text").each(function() {
arr_.push($(this).val())
})
$(":text").focusin(function(){
var that = this;
var value = $(that).val();
$.each(arr_,function(i,n) {
if(value==n){
$(that).val("");
if(value=="请输入密码"||value=="确认密码"){
$(that).attr("type","password");
}
}
});
})
$(":input").focusout(function(event) {
/* Act on the event */
if($(this).val() == "") {
if ($(this).hasClass('pwd')) {
$(this).attr('type','text')
};
$(this).val($(this).attr("data"));
}
});
})

这样便看起来舒服多了。

jQuery操作input改变value属性值的更多相关文章

  1. jquery获取、改变元素属性值

    //标签的属性称作元素属性,在JS里对应的DOM对象的对应属性叫DOM属性.JS里的DOM属性名有时和原元素属性名不同. //==================================操作元 ...

  2. python 全栈开发,Day54(jQuery的属性操作,使用jQuery操作input的value值,jQuery的文档操作)

    昨日内容回顾 jQuery 宗旨:write less do more 就是js的库,它是javascript的基础上封装的一个框架 在前端中,一个js文件就是一个模块 一.用法: 1.引入包 2.入 ...

  3. jQuery 操作 input 之 checkbox

    jQuery 操作 input 之 checkbox 一片 HTML 清单: <input type="checkbox" name="hobby" va ...

  4. jquery获取当前选项的属性值a

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. jquery:jqery表单属性 值操作

    重置表单(且清空隐藏域)  $('#myform')[0].reset() ​​ DOM属性相关操作 返回属性值 $(selector).attr(attribute) 设置属性值 $(selecto ...

  6. jQuery -- 光阴似箭(三):jQuery 操作 HTML 元素和属性

    jQuery -- 知识点回顾篇(三):jQuery拥有操作 HTML 元素和属性的强大方法. 1. 获取HTML 元素的内容和属性 (1) 获得内容:  text().html() 以及 val() ...

  7. jquery取<input>的readOnly属性,O要大写

    今天在jquery中取input的readonly属性时,发现 我这样写$(“#input”).readonly取这个属性时,总是undefined,后来一想,难道html中的属性only没有大写,是 ...

  8. jQuery操作<input type="radio">

    input type="radio">如下: <input type="radio" name="city" value=&qu ...

  9. jQuery操作元素的class属性

    今天在做一个菜单折叠插件的时候需要根据页面的url改变其class属性.jQuery操作元素的属性是非常方便的,在此记录一下: 1.给元素添加class属性: addClass(class) (1)添 ...

随机推荐

  1. eclipse查看类源码出现failed to create the part's controls的解决方法

    刚装好eclipse和Tomcat ,在看准备使用的一个开源框架jeesite,可是在查看源代码的时候出现这个问题

  2. TensorFlow Object Detection API(Windows下训练)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近事情比较多,前面坑挖的有点久,今天终于有时间总结一下,顺便把Windows下训练跑通.Li ...

  3. web service实例

    一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...

  4. ZOJ 3777-Problem Arrangement(状压DP)

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  5. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  6. 暑假练习赛 003 F Mishka and trip

    F - Mishka and trip Sample Output   Hint In the first sample test: In Peter's first test, there's on ...

  7. 数位DP按位枚举模板

    借鉴:http://www.cnblogs.com/xz816111/p/4809913.html // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位 ...

  8. JQuery源码-------JQuery中数值型变量的判断isNumeric

    判断一个数值型变量的方法,在jquery中非常简单,只有一行代码. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false ...

  9. R-CNN论文翻译——用于精确物体定位和语义分割的丰富特征层次结构

    原文地址 我对深度学习应用于物体检测的开山之作R-CNN的论文进行了主要部分的翻译工作,R-CNN通过引入CNN让物体检测的性能水平上升了一个档次,但该文的想法比较自然原始,估计作者在写作的过程中已经 ...

  10. table固定头部,表格tbody可上下左右滑动

    当表格头部固定时,需要分为两个表格来做:一部分是thead,一部分是tbody,具体实现方式如下: html代码: <div class="table_box_big"> ...