Javascript条件输入id语句
Javascript condition input id statement
我让这段代码完美地工作,以模拟带有图像的预填充输入字段:
<script>
$('#example).focus(function(){
var newValue = $(this).val();
if ($(this).val() == ""){
$(this).css("background-image", "none");
}else{
$(this).val(newValue);
}
}).blur(function(){
var newValue = $(this).val();
if ($(this).val() == ""){
$(this).css("background-image", "url(assets/templates/herveou-coiffure/css/pre_input.png)");
}else{
$(this).val(newValue);
}
});
</script>
我需要使用这个与几个输入字段(至少2),所以我尝试了这样的东西:
<script>
$('#example, example2').focus(function(){
var newValue = $(this).val();
if ($(this).val() == ""){
$(this).css("background-image", "none");
}else{
$(this).val(newValue);
}
}).blur(function(){
var newValue = $(this).val();
if ($(this).val() == ""){
if ($('#example2')[0]){
// Do something here if an element with this class exists
$(this).css("background-image", "url(assets/templates/herveou-coiffure/css/headers.jpg)");}
if ($('#example')[0]){
// Do something here if an element with this class exists
$(this).css("background-image", "url(assets/templates/herveou-coiffure/css/pre_input.png)");}
}else{
$(this).val(newValue);
}
});
</script>
当然我不是javascript专家,它不起作用,任何人都可以帮助??
每次选择ID时,都需要记住使用ID选择器#
。
你需要用$('#example, #example2')
代替$('#example, example2')
当你写$('<selector1>, <selector2> , ....'
。您应该在每个参数中编写适当的选择器。所以你应该把$('#example, example2')
改成$('#example, #example2').
相关文章:
- 如何重写Javascript If语句以选择Classes而不是ID's的HTML格式
- JqueryUI,多个ID和else if 语句在可丢弃,我如何让它们工作
- jQuery if 语句未捕获 #ID
- 如何在纯 javascript 中检查元素是否有 ID?和条件语句问题
- 从 select 中获取 ID,并在 if 语句中使用它们
- 按 ID 的 JS IF 语句
- 在 if else 语句中传递 id 属性
- 正在添加项目id's到js-if语句
- 获取语句中表行的ID
- 在 jQuery UI 中按 id 切换语句以显示不同的菜单
- 如何在switch语句中使用id,并在单击按钮时提醒消息
- 按id更改多个列表项的类的条件语句
- Jquery简化了与多个id相关的多个if语句,这些id具有相似的字符
- 如何使用 JavaScript 在 if 语句中使用按钮的 ID
- 如何使用switch语句设置变量的值,该语句的表达式是作为dom元素id设置的变量
- 链接到id(哈希),检测哈希值,但scrollTo不通过if语句触发固定导航
- Javascript条件输入id语句
- 我应该使用switch语句将CSS id与JavaScript变量关联起来吗?
- If和else语句用于id和class更改页边距
- 如何在Jquery中为if语句选择一个ID