2016-05-25 78 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',"+"); 
    }); 
});