JQuery 添加 class 到元素后隐藏不起作用

JQuery hide not working after adding class to element

我目前正在使用 Ajax、JQuery 开发 php 应用程序。我在使用 JQuery 显示我的内容时遇到了问题。

<div class="table-responsive">
    <table class="table table-hover table-dark" id="table_beam"></table>
</div>

我可以使用

隐藏我的 table
$('#table_beam').hide();

或者我可以使用

隐藏整个 div
$('.table-responsive').hide();

但是一旦我将 类 添加到我的 table 中,如下所示:

<div class="table-responsive top tg">
    <table class="table table-responsive w-100 d-block d-md-table table-hover table-dark" id="table_beam"></table>
</div>

我无法再使用 id 元素隐藏

$('#table_beam').hide();

嘿,几个小窍门或许能帮到你,

1. hide()

也许你应该把 hide()duration 的第一个参数默认设置为 400 所以试试这个:

$('#table_beam').hide(0);

2。直接设置ce CSS

您可以像这样直接设置 CSS 样式:$('#table_beam').css('display', 'none');

3。 CSSClass

我最喜欢的方法,因为根据我的说法,如果你不必在 hide() 之后调用回调,那么只需设置 class

main.css : .table-hide {display: none}

您可以这样设置:$('#table_beam').addClass(table-hide); 并像这样删除:$('#table_beam').removeClass(table-hide);

希望对您有所帮助:)

EDIT :我认为这个 class d-block 设置 display: block !important,所以使用我的第三种方法和以下内容:main.css : #table_beam.table-hide {display: none !important}

class .d-block 有 css display:block!important 这将不允许 .hide() 覆盖显示。请删除此 class 并找到一些替代方法。

希望对您有所帮助