Javascript 为焦点函数组合到元素
如何组合两个元素以使用焦点功能#s#u type和s#u ctry我正试图通过两次重新键入相同的内容来避免额外的代码 我现在尝试的方式不起作用Javascript 为焦点函数组合到元素,javascript,jquery,Javascript,Jquery,如何组合两个元素以使用焦点功能#s#u type和s#u ctry我正试图通过两次重新键入相同的内容来避免额外的代码 我现在尝试的方式不起作用 <script type="text/javascript"> $(document).ready(function() { $("#s_type", "#s_ctry").focus(function() { var first = $(this).find("option").eq(0); if(f
<script type="text/javascript">
$(document).ready(function() {
$("#s_type", "#s_ctry").focus(function() {
var first = $(this).find("option").eq(0);
if(first.val() === "0") {
first.remove();
}
});
});
</script>
$(文档).ready(函数(){
$(“#s#u type”,“#s#ctry”)。焦点(函数(){
var first=$(this.find(“option”).eq(0);
if(first.val()=“0”){
首先,删除();
}
});
});
您必须在选择器本身中使用逗号:
$("#s_type, #s_ctry")
选择器本身必须使用逗号:
$("#s_type, #s_ctry")
你就快到了!您需要将它们组合成一个字符串。将两个参数传递给jQuery时,第二个参数是要匹配选择器的上下文,不能将多个选择器作为单独的参数提供:
$("#s_type, #s_ctry").focus(function() {
//Do stuff!
});
或者,可以使用该方法将元素添加到匹配集:
$("#s_type").add("#s_ctry").focus(function() {
//Do stuff!
});
你就快到了!您需要将它们组合成一个字符串。将两个参数传递给jQuery时,第二个参数是要匹配选择器的上下文,不能将多个选择器作为单独的参数提供:
$("#s_type, #s_ctry").focus(function() {
//Do stuff!
});
或者,可以使用该方法将元素添加到匹配集:
$("#s_type").add("#s_ctry").focus(function() {
//Do stuff!
});