val()方法

3.val()方法 

此方法类似于JavaScript中的value属性,可以用来设置和获取元素的值 

无论元素时文本框,下拉列表还是单选框,它都可以返回元素的值 。

如果元素为多选,则返回一个包含所有选择的值的数组 

HTML 代码如下:

<input type="text" id="address" value="请输入邮箱地址"/>
<input type="text" id="password" value="请输入邮箱密码"/>
<input type="button" value="登录">


第2步:对"地址框"进行操作 


当地址框获取鼠标焦点时,如果地址框的值为"请输入邮箱地址",则将地址框中的值清空。

可以使用如下的jQuery代码:

node2:/var/www/html/jquery#cat t36.html 
<input type="text" id="address" value="请输入邮箱地址"/>
<input type="text" id="password" value="请输入邮箱密码"/>
<input type="button" value="登录">
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>    
<script type="text/javascript" src="t36.js"></script>




node2:/var/www/html/jquery#cat t36.js
$("#address").focus(function(){
        var txt_value = $(this).val(); //获取地址文本框的值
        if(txt_value=='请输入邮箱地址'){
            $(this).val('');
        }
});

猜你喜欢

转载自blog.csdn.net/zhaoyangjian724/article/details/89885742
val