javascript - 是否可以在 jQuery 中将多个 HTML 元素合并到一个脚本中?

标签 javascript jquery html

我在 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/

相关文章:

javascript - 为什么 jQuery 设置 jQuery 禁用属性仅适用于第一个单选输入?

javascript - 如何处理在 HTML5 Canvas 上绘制的形状并更改其属性?

javascript - knockout 可观察数组中的错误值

javascript - 如何在 react js 中嵌入 google adsense

javascript - jquery ajax选项卡点击后更改为 "Loading..."

javascript - 使用javascript捕获溢出文本高度

javascript - 将 div 放在中心并在用户尝试调整窗口大小时更新函数

javascript - 移动多个元素并将它们放回原位

javascript - 如何正确使用 postMessage 与 html5 和现代浏览器进行跨域消息传递?我仍然收到错误

javascript - Typescript:TS 编译器不接受使用 bool 类型