2016-05-25 80 views
0

我得到这个jQuery脚本,它把占位符“+”在我的搜索输入,并且probblem的是,现在每输入了我的网站“+”占位符,jQuery的添加类输入

如何只添加格外类这个JS代码:

$(document).ready(function() { 
    $('input').on('focus',function(){ 
    $(this).attr('placeholder',""); 
}); 
$('input').on('blur',function(){ 
    $(this).attr('placeholder',"+"); 
}); 

}); 
+0

只需在'$(this)'中用类调用'addClass'? [jQuery addClass](https://api.jquery.com/addclass/) –

+1

将类名置于选择器中......'$(“input.className”)' – Archer

+0

由于'Archer'表示可以选择类似这个'$(“input.myClass [type = text]”)' – Ramkee

回答

0

要更新placeholder

希望使用像classid标识符这个片段将是有用的

JS

$(document).ready(function() { 
    $('input').on('focus',function(){ 
    $(this).attr('placeholder',""); 
}); 
// add placeholder to input which have class updatePlaceholder 
$('input.updatePlaceholder').on('blur',function(){ 
    $(this).attr('placeholder',"+"); 
}); 

}); 

HTML

<input type = "text"> 
<input type = "text" class = "updatePlaceholder"> 

入住这jsFiddle

1

这是很简单的jQuery代码。谷歌搜索会找到你的答案。您只需指定类名称:

$(document).ready(function() { 
    $('input.CLASSNAME').on('focus',function(){ 
    $(this).attr('placeholder',""); 
    }); 
    $('input.CLASSNAME').on('blur',function(){ 
    $(this).attr('placeholder',"+"); 
    }); 
});