gpt4 book ai didi

Javascript条件输入id语句

转载 作者:行者123 更新时间:2023-11-30 10:32:14 24 4
gpt4 key购买 nike

我让这段代码完美运行,以模拟带有图像的预填充输入字段:

<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>

当然我不是 javascritp 专家而且它不起作用,任何人都可以提供帮助??非常感谢...

最佳答案

每次选择 ID 时,都需要记住使用 ID 选择器 #

您需要使用 $('#example, #example2') 而不是 $('#example, example2')

关于Javascript条件输入id语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com