如果没有 .show(),代码将无法工作 - jQuery

发布于 2024-08-14 09:21:42 字数 477 浏览 2 评论 0原文

    this.randomtip = function(){
        var length = $("#showcase ul li").length;
        var ran = Math.floor(Math.random()*length) + 1;
        $("#showcase ul li:nth-child(" + ran + ")").show();
    };
    randomtip();

..上面的代码最终与 .show() 配合得很好,但如果我将其更改为 .addClass('show') 它将无法工作。

我想给它一个类,而不是将 float:left 属性应用于所有 li 项目,而不是默认的 display:list-item 。我该如何解决这个问题?

谢谢!

    this.randomtip = function(){
        var length = $("#showcase ul li").length;
        var ran = Math.floor(Math.random()*length) + 1;
        $("#showcase ul li:nth-child(" + ran + ")").show();
    };
    randomtip();

..the code above works well with .show() in the end, but if I change it to .addClass('show') it won't work.

I'd like to give it a class instead to apply float:left property to all li items instead of the default display:list-item. How do I fix this?

Thanks!

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

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

发布评论

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

评论(4

挽手叙旧 2024-08-21 09:21:42

在乔丹的回答之后:

您可以替换 li 中的 display:none ,然后以这种方式添加类:

$("#showcase ul li:nth-child(" + ran + ")").css('display','').addClass('show');

After Jordan's answer:

You can replace the display:none from li and then add the class this way:

$("#showcase ul li:nth-child(" + ran + ")").css('display','').addClass('show');
烟织青萝梦 2024-08-21 09:21:42

您首先如何隐藏该元素?例如,如果您将其设置为 display: none,或者使用 jQuery 的 hide()(它执行相同的操作),那么这将覆盖其他地方定义的 CSS 并保持您的元素隐藏,直到您将其删除。如果您同时使用 .show() (删除 display: none)和 .addClass('show') (删除 display: none),它应该可以工作。添加您的 CSS 类)。

How are you hiding the element in the first place? If you're setting it to display: none, for example, or using jQuery's hide(), which does the same thing, then that will override CSS defined elsewhere and keep your element hidden until you remove it. It ought to work if you use both .show() (to remove display: none) and .addClass('show') (to add your CSS class).

ㄟ。诗瑗 2024-08-21 09:21:42

尝试

$("#showcase ul li:nth-child(" + ran + ")").show();

用这个替换

$("#showcase ul li:nth-child(" + ran + ")").addClass('show').show();

Try replacing

$("#showcase ul li:nth-child(" + ran + ")").show();

with this one

$("#showcase ul li:nth-child(" + ran + ")").addClass('show').show();
电影里的梦 2024-08-21 09:21:42

使用 .addClass('hidden') / .removeClass('hidden') 代替 display: none

instead of display: none, use .addClass('hidden') / .removeClass('hidden')

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