jQuery操作input改变value属性值
今天写了一个表单元素,在用户点击的时候会清空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属性值的更多相关文章
- jquery获取、改变元素属性值
//标签的属性称作元素属性,在JS里对应的DOM对象的对应属性叫DOM属性.JS里的DOM属性名有时和原元素属性名不同. //==================================操作元 ...
- python 全栈开发,Day54(jQuery的属性操作,使用jQuery操作input的value值,jQuery的文档操作)
昨日内容回顾 jQuery 宗旨:write less do more 就是js的库,它是javascript的基础上封装的一个框架 在前端中,一个js文件就是一个模块 一.用法: 1.引入包 2.入 ...
- jQuery 操作 input 之 checkbox
jQuery 操作 input 之 checkbox 一片 HTML 清单: <input type="checkbox" name="hobby" va ...
- jquery获取当前选项的属性值a
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- jquery:jqery表单属性 值操作
重置表单(且清空隐藏域) $('#myform')[0].reset() DOM属性相关操作 返回属性值 $(selector).attr(attribute) 设置属性值 $(selecto ...
- jQuery -- 光阴似箭(三):jQuery 操作 HTML 元素和属性
jQuery -- 知识点回顾篇(三):jQuery拥有操作 HTML 元素和属性的强大方法. 1. 获取HTML 元素的内容和属性 (1) 获得内容: text().html() 以及 val() ...
- jquery取<input>的readOnly属性,O要大写
今天在jquery中取input的readonly属性时,发现 我这样写$(“#input”).readonly取这个属性时,总是undefined,后来一想,难道html中的属性only没有大写,是 ...
- jQuery操作<input type="radio">
input type="radio">如下: <input type="radio" name="city" value=&qu ...
- jQuery操作元素的class属性
今天在做一个菜单折叠插件的时候需要根据页面的url改变其class属性.jQuery操作元素的属性是非常方便的,在此记录一下: 1.给元素添加class属性: addClass(class) (1)添 ...
随机推荐
- Centos7安装后出现please make your choice from '1' to e 解决方式
[输入"1",按Enter键 输入"2",按Enter键 输入"q",按Enter键 输入"yes",按 ...
- Zookeeper 笔记-watch
ZooKeeper对Watch提供了什么保障 对于watch,ZooKeeper提供了这些保障: Watch与其他事件.其他watch以及异步回复都是有序的. ZooKeeper客户端库保证所有事件都 ...
- sql里的null和空的区别
null表示为未知,未定义: 空表示为空白,或者0: sql查询,排序时null在''的前面: 定义字段为not null,写为空可以写入: null不可以用来比较,只能用is null判断:
- ASP.NET Core 2.0 in Docker on Windows Container
安装Docker for Windows https://store.docker.com/editions/community/docker-ce-desktop-windows 要想将一个ASP. ...
- Vue 国际化 vue-i18n 用法详解
vue-i18n 仓库地址:https://github.com/kazupon/vue-i18n 兼容性: 支持 Vue.js 2.x 以上版本 安装方法:(此处只演示 npm) npm insta ...
- 2_认识STM32库
2_认识STM32库 STM32库是由ST公司针对STM32提供的函数接口API,开发者可以调用这些函数接口来配置STM32的寄存器,使得开发人员得以脱离最底层的寄存器操作,开发快速. 库是架设在寄存 ...
- Problem V
Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that th ...
- 多项式求和,素数判定 HDU2011.2012
HDU 2011:多项式求和 Description 多项式的描述如下: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... 现在请你求出该多项式的前n项的和. Input ...
- Mybatis动态查询语句
MyBatis中动态SQL语句完成多条件查询 标签: mybatis动态SQL多条件查询java.sql.SQLSyntaxEr 2015-06-29 19:00 22380人阅读 评论(0) 收藏 ...
- 五种js判断是否为整数(转)
五种js判断是否为整数类型方式 作者:snandy 这篇文章主要介绍了五种JavaScript判断是否为整数类型方式,需要的朋友可以参考下 这篇看看如何判断为整数类型(Integer),JavaS ...