使用jQuery的重点来清空文本字段 [英] using jquery focus to empty text fields

查看:152
本文介绍了使用jQuery的重点来清空文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将焦点添加到以下代码中,该代码将循环访问10个或更多个字段,并在用户将焦点放在字段上时删除默认值。
如何将焦点添加到现有的代码?
-thanks

  $(document).ready(function(){
$ ').click(function(){
$('#form-container')。attr(class,trade);
$('。lblMadlib11')。text(Eleven );
$('。lblMadlib11a').val(11 value field);
});


解决方案

假设这个标记是这样的:

Html:

 < input type =textplaceholder =default text/> 

Javascript:

  $(function(){// onload 
$('input:text')。focus(function(){// onfocus
if($(this).val()== $(this).attr('placeholder'))// if值是默认值
$(this).val(''); //清除值
});
});
pre>

I am trying to add focus on the following code, which would loop through the 10 or more fields and remove the default value(s) when the user focuses on the field. how can i add the focus to the existing code? -thanks

$(document).ready(function() {  
    $('#tradition').click(function() {
    $('#form-container').attr("class","trade");                         
    $('.lblMadlib11').text("Eleven ");
    $('.lblMadlib11a').val("eleven value field");
});

解决方案

Assuming this markup this:

Html:

<input type="text" placeholder="default text" />

Javascript:

$(function(){ // onload
    $('input:text').focus(function(){ // onfocus
       if($(this).val() == $(this).attr('placeholder')) // if value is default value
           $(this).val(''); // clear value
    });
});

这篇关于使用jQuery的重点来清空文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆