javascript - 如何减少Document.getelementbyid

标签 javascript jquery

这里的代码包含大量document.getelementbyid,用于简化列表框,以便在单击JavaScript中的单选按钮时显示和隐藏,现在我正在尝试减少代码,但作为jquery的初学者一路学习,有人能帮助减少 jquery 中的这种情况吗?

<script language="JavaScript" type="text/javascript">
function fun(s) {
    /* table 1 */
    if (s == "maingroup") {
        document.getElementById("subgroup").value = null;
        document.getElementById("itemname").value = null;

        document.getElementById("subgroup_value").value = null;
        document.getElementById("itemname_value").value = null;

        document.getElementById("maingroup").style.display = '';
        document.getElementById("subgroup").style.display = 'none';
        document.getElementById("itemname").style.display = 'none';
    }
}
<script>

JSFIDDLE

最佳答案

 $("#subgroup,#itemname,#subgroup_value,#itemname_value").val('');
 $("#maingroup,#subgroup,#itemname").hide();

关于javascript - 如何减少Document.getelementbyid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21545082/

相关文章:

jquery - 如何避免垂直拖动选择?

javascript - jQuery Uncaught TypeError with offset().left

javascript - 如何在同一类、id 和名称中的字段中获取最高值文本?

javascript - future 的安全措施会限制 JSONP 的跨域异步能力吗?

javascript - 在数组中存储 Highcharts 问题

javascript - Bootstrap 5 排对齐按钮

javascript - 为什么我不能在 jQuery 中解析 A​​jax html GET 响应?

javascript - 如何在 Angular 页面中接收 GET 请求?

javascript - 上滑下滑问题

javascript - 按类搜索元素并在 jquery 中为每个元素添加 id