我在 HTML 中使用了多个元素,脚本是相同的。那么是否可以在 jQuery 中将多个 HTML 元素合并到一个脚本中呢?代码越短越容易管理。
future :
$("#edit_rules_form input select textarea").each(function(){
if(!$(this).val()){
$(this).tooltip("show");
checkfieldvalue = false;
$(this).on("focus", function(){
$(this).tooltip("hide");
});
}
});
当前 jQuery:
$("#edit_rules_form input").each(function(){
if(!$(this).val()){
$(this).tooltip("show");
checkfieldvalue = false;
$(this).on("focus", function(){
$(this).tooltip("hide");
});
}
});
// Tooltip for <select> dropdown
$("#edit_rules_form select").each(function(){
if(!$(this).val()){
$(this).tooltip("show");
checkfieldvalue = false;
$(this).on("focus", function(){
$(this).tooltip("hide");
});
}
});
// Tooltip for <textarea> box
$("#edit_rules_form textarea").each(function(){
if(!$(this).val()){
$(this).tooltip("show");
checkfieldvalue = false;
$(this).on("focus", function(){
$(this).tooltip("hide");
});
}
});
最佳答案
您可以使用逗号
编写多个选择器 - https://api.jquery.com/multiple-selector/ .
$("#edit_rules_form input, #edit_rules_form select, #edit_rules_form textarea").each(function(){
if(!$(this).val()){
$(this).tooltip("show");
checkfieldvalue = false;
$(this).on("focus", function(){
$(this).tooltip("hide");
});
}
});
关于javascript - 是否可以在 jQuery 中将多个 HTML 元素合并到一个脚本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441535/