为什么 show() 只适用于使用内联 css 隐藏的字段?

发布于 2024-08-25 20:06:43 字数 492 浏览 4 评论 0原文

我使用内联 css 隐藏一个元素,如下所示:

<span class="hidden-nojs" style="display:none">Some text</span>

接下来我使用 jQuery 来显示该元素,如下所示:

$(".hidden-nojs").show();

这效果很好。一旦我删除内联 css 并将 display:none 放在隐藏的 nojs 类的外部 css 样式表上,它就会停止工作。这是我在外部样式表中写的:

.hidden-nojs {
    display: none;  
}

我假设外部样式表在 jQuery 已经运行后加载?这有点烦人,因为我想用 css 隐藏多个元素,并希望避免使用内联 css。

为什么 show() 只适用于使用内联 css 隐藏的字段?我该如何解决这个问题?

I am hiding an element using inline css, like so:

<span class="hidden-nojs" style="display:none">Some text</span>

Next I use jQuery to show the element, like so:

$(".hidden-nojs").show();

This works great. As soon as I remove the inline css and put display:none on the external css stylesheet for the hidden-nojs class, it stops working. This is what I wrote in the external stylesheet:

.hidden-nojs {
    display: none;  
}

I'm assuming that the external stylesheet loads after the jQuery has already run? This is somewhat annoying as I would like to hide multiple elements with css and would like to avoid using inline css.

Why will show() only work for fields that are hidden using inline css? How can I fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2024-09-01 20:06:43

问题是您试图隐藏和显示一个内联元素。 jQuery 效果仅适用于块类型元素。 jQuery 效果操纵内联元素样式,这就是该版本有效而​​类版本无效的原因。

如果您考虑一下,块类型元素就是矩形。它们很容易制作生长和缩小的动画,因为您只需制作它们的高度和/或宽度的动画即可。

内联元素不一定是矩形,这就是 jQuery 效果大部分不起作用的原因。举个例子:

<p>Inside paragraph <span class="foo">This is a test</span> contains text.</p>
<input type="button" id="bar" value="Toggle">

运行:

$(function() {
  $("#bar").click(function() {
    $("span.foo").toggle();
  });
});

第一次单击将隐藏它。第二个会将其变成一个块(因为它将不再包含在段落中)。这就是我所说的 jQuery 效果并不真正适用于内联元素的意思。

在内部,jQuery 效果通过动画和在 display: nonedisplay: block 之间切换的组合来工作。这意味着 jQuery 效果也不适用于表格单元格。为什么?它们不是 display: block,它们实际上是 display: table-cell。您自己尝试一下,看看多次 toggle() 表格单元格后会发生什么。

那么解决办法是什么呢?对于内联内容,最简单的解决方案就是使用类:

hidden { display: none; }

with

$("span.hideme").toggleClass("hidden");

这没有动画效果。您可以自己添加它,但我不能 100% 保证 animate() 与内联元素的配合效果。

或者,如果您的内容可以成为一个块,那么将其设为一个块,所有这些问题都会消失。在您的情况下,可以使用

而不是 或将 display: block 添加到 <跨度>

The problem is you're trying to hide and show an inline element. The jQuery effects only work with block type elements. The jQuery effects manipulate inline element style, which is why that version works and the class version doesn't.

If you think about it block type elements are rectangles. They're easy to animate growing and shrinking because you simply animate their height and/or width.

Inline elements aren't necessarily rectangles, which is why the jQuery effects mostly don't work. To give you an example:

<p>Inside paragraph <span class="foo">This is a test</span> contains text.</p>
<input type="button" id="bar" value="Toggle">

Run:

$(function() {
  $("#bar").click(function() {
    $("span.foo").toggle();
  });
});

The first click will hide it. The second will turn it into a block (as in it will no longer be contained in the paragraph). This is what I mean about jQuery effects not really working with inline elements.

Internally, the jQuery effects work by a combination of animation and toggling in between display: none and display: block. What this means is jQuery effects don't work on table cells either. Why? They're not display: block, they're effectively display: table-cell. Try this yourself and see what happens after you toggle() a table cell a couple of times.

So what's the solution? For inline content the easiest solution is simply to use classes:

hidden { display: none; }

with

$("span.hideme").toggleClass("hidden");

This doesn't have the animation effect. You can add this yourself but I'm not 100% how well animate() will work with inline elements.

Alternatively, if your content can be a block, make it a block and all these problems go away. In your case either use a <div> instead of a <span> or add display: block to the <span>.

秋凉 2024-09-01 20:06:43

我无法复制这个问题,但我认为这与 Cletus 所说的没有太大关系。这里没有动画吗?

将样式内联与它是内联元素这一事实无关。我过去在使用自制切换可见性脚本和类时遇到过类似的问题。许多简单的实现(例如我的)的工作方式是这样的:

function toggle(el) {
    el.style.display = (el.style.display == 'none') ? '' : 'none';
}

如果它不是隐藏的,请将内联样式更改为隐藏。如果隐藏,删除内联样式。当您删除内联样式时,它会恢复为样式表中定义的样式,这就是它不显示的原因。

就像我说的,我尝试使用 jQuery 的 show() 函数复制您的错误,但无法做到。在我的测试中,jQuery 足够聪明,可以恢复到之前的显示样式,所以我不知道问题到底是什么,但听起来很像我之前遇到的问题。

就解决方案而言,只需删除 hidden-nojs 类即可。

I haven't been able to replicate this problem, but I don't think it's anything much to do with what Cletus is talking about. There's no animation here?

Putting a style inline is nothing to do with the fact that it's an inline element. I've had similar problems in the past when using a homebrew toggle-visibility script and classes. The way that many naive implementations (such as mine) worked is like this:

function toggle(el) {
    el.style.display = (el.style.display == 'none') ? '' : 'none';
}

If it's not hidden, change the inline style to hidden. If it is hidden, remove the inline style. When you remove the inline style, it then reverts to style defined in your stylesheet, which is why it doesn't get shown.

Like I said, I tried replicating your error using jQuery's show() function and couldn't do it. In my tests, jQuery is smart enough to revert to what the display style was before, so I don't know exactly what the problem is, but it sounds a lot like the problem I had before.

As far as a solution goes, just remove the hidden-nojs class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文