简体   繁体   English

使用jQuery交换输入字段占位符文本

[英]Swapping input field placeholder text with jQuery

I try as following jquery code, but it doesn't work true, how can fix it? 我尝试按照以下jquery代码进行操作,但无法正常运行,如何解决?

Demo: http://jsfiddle.net/pjqcf/ 演示: http //jsfiddle.net/pjqcf/

<input type="text" value="Some descriptive text...">​

$('input[type=text]').focus(function () {
    if ($(this).val() == $(this).attr('defaultValue')) {
        $(this).val('');
    }
});
$('input[type=text]').blur(function () {
    if ($(this).val() == '') {
        $(this).val($(this).attr('defaultValue'));
    }
});​

You haven't set "defaultValue" attribute of your input. 您尚未设置输入的“ defaultValue”属性。 Set "defaultValue" to "Some descriptive text.." and it should work 将“ defaultValue”设置为“一些描述性文本..”,它应该可以工作

<input type="text" value="Some descriptive text..." defaultValue="Some descriptive text...">​

Works fine after adding defaultValue to your markup. 将defaultValue添加到标记后,效果很好。 See below change, 看到下面的变化,

<input type="text" 
    value="Some descriptive text..." 
    defaultValue="Some descriptive text...">

DEMO 演示

change "defaultValue" to "value". 将“ defaultValue”更改为“ value”。 Or add defaultValue attribute to your input 或将defaultValue属性添加到您的输入中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM