使用JQuery隐藏占位符值

时间:2013-06-09 10:34:03

标签: jquery placeholder

我有很多带有一些文字输入的模板,如下所示:

<input type="text" autocomplete="off" value="" placeholder="Keywords" name="keywords" id="keywords">

当用户点击该字段时,客户端要求我隐藏占位符值(示例中的关键字)。我不知道如何用JS和JQuery做到这一点。有没有办法通过crossbrowser解决方案实现这一目标?

1 个答案:

答案 0 :(得分:10)

试试这个:{检查是否跨浏览器}

http://jsfiddle.net/MDhtj/1

$('input[placeholder]').on('focus', function () {
    var $this = $(this);
    $this.data('placeholder', $this.prop('placeholder')).removeAttr('placeholder')
}).on('blur', function () {
    var $this = $(this);
    $this.prop('placeholder', $this.data('placeholder'));
});