JQuery使用id,但不使用类
我正在使用jQuery作为搜索栏。未选中时写入“搜索…”。我遇到的问题是,当我使用“class”时jQuery不起作用,但当我使用“id”时它起作用。我想知道是什么问题 以下是我的代码: HtmlJQuery使用id,但不使用类,jquery,html,Jquery,Html,我正在使用jQuery作为搜索栏。未选中时写入“搜索…”。我遇到的问题是,当我使用“class”时jQuery不起作用,但当我使用“id”时它起作用。我想知道是什么问题 以下是我的代码: Html 搜索 You code works,我已经添加了代码段,你能复制它吗?$(.search-form\u input)很可能会返回一个数组,因此Jquery可能会因为尝试$(.search-form\u input”)[0]而出错。val(“search…”)。addClass(“empty”)你有多
搜索
You code works,我已经添加了代码段,你能复制它吗?$(.search-form\u input)很可能会返回一个数组,因此Jquery可能会因为尝试$(.search-form\u input”)[0]而出错。val(“search…”)。addClass(“empty”)你有多个.search-form\u input
类吗?@Satpal你在哪里添加了代码段?@宣誓就行了。谢谢。@不,这是唯一的一个。在问题中,你看不到“显示代码片段”吗?
<form class="search-form" action="search.php" method="GET" accept-charset="utf-8">
<label class="search-form_label">
<input class="search-form_input" id="search" type="text" name="s" autocomplete="off" placeholder=""/>
<span class="search-form_liveout"></span>
</label>
<button class="search-form_submit fa-search" type="submit">search</button>
</form>
<script>
$(document).ready(function(){
// Add the value of "Search..." to the input field and a class of .empty
$("#search").val("Search...").addClass("empty");
// When you click on #search
$("#search").focus(function(){
// If the value is equal to "Search..."
if($(this).val() == "Search...") {
// remove all the text and the class of .empty
$(this).val("").removeClass("empty");;
}
});
// When the focus on #search is lost
$("#search").blur(function(){
// If the input field is empty
if($(this).val() == "") {
// Add the text "Search..." and a class of .empty
$(this).val("Search...").addClass("empty");
}
});
});
</script>
<script>
$(document).ready(function(){
// Add the value of "Search..." to the input field and a class of .empty
$(".search-form_input").val("Search...").addClass("empty");
// When you click on #search
$(".search-form_input").focus(function(){
// If the value is equal to "Search..."
if($(this).val() == "Search...") {
// remove all the text and the class of .empty
$(this).val("").removeClass("empty");;
}
});
// When the focus on #search is lost
$(".search-form_input").blur(function(){
// If the input field is empty
if($(this).val() == "") {
// Add the text "Search..." and a class of .empty
$(this).val("Search...").addClass("empty");
}
});
});
</script>