javascript - 如何使用类隐藏元素;而不是在 JQuery 中使用 css 函数?

标签 javascript jquery html css

解决方案中多次使用数据表。

在一个地方,我们遇到了需要隐藏数据表标题的要求:

<div id="DocTable_wrapper" class="dataTables_wrapper no-footer">
<div>
 <div class="dataTables_scroll">
  //Following is top div of head which is generated dynamically by datatables
  <div class="dataTables_scrollHead noDisplay" style="overflow: hidden; position: relative; border: 0px none; width: 100%;">
     <div class="dataTables_scrollHeadInner" style="box-sizing: content-box; width: 1188px; padding-right: 16px;">
       <table class="dataTable no-footer" role="grid" style="margin-left: 0px; width: 1188px;">
          <thead id="DocHeading">
          </thead> 

我在 dataTables_scrollHead 中添加了一个名为 noDisplay 的样式。

它包含:

noDisplay{
display:none !important;
}

我在 JQuery 中将其应用为:

$(".dataTables_scrollHead").addClass("noDisplay");

当我运行代码时,标题不会隐藏。 在 Firebug 中我可以看到样式名称。在 Firebug 中,当我尝试将类应用于其下面的 div 时;它没有被应用。

在firebug中,当我在样式标签中使用内联样式display:none时。它隐藏起来。

但是当我使用时

$(".dataTables_scrollHead").css('display', 'none');    

成功了。

为什么?

根据标准,我们不应该使用 .css 而是仅使用 addClass。有人请帮我使用 addclass 做同样的事情吗?

最佳答案

也许你的 CSS 中有一个拼写错误?

你的CSS:

noDisplay{
display:none !important;
}

你忘记了:

.noDisplay{
display:none !important;
}

EXAMPLE

关于javascript - 如何使用类隐藏元素;而不是在 JQuery 中使用 css 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32098525/

相关文章:

javascript - 移动时的菜单不会滚动

javascript - 处理请求时使屏幕变黑

javascript - 如何禁用所有浏览器的 razor 组件中使用的 `<InputText type="password">` 字段中显示的建议密码列表

javascript - 使用 Greasemonkey 仅定位非事件按钮

html - 如何使用免费标记获取列表中的第一项?

Jquery:如何使用 :not(class) 选择 :first-child

javascript - 无法从动态填充的字符串中 trim 尾随逗号

javascript - 如何用正则表达式匹配和替换所有不是数字或破折号的字符作为第一个字符?

javascript - 谷歌地图 API : Create a store locator

javascript - 即时使用 js 或 jquery 更改元素 css 样式