如果没有 .show(),代码将无法工作 - jQuery
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在乔丹的回答之后:
您可以替换
li
中的display:none
,然后以这种方式添加类:After Jordan's answer:
You can replace the
display:none
fromli
and then add the class this way:您首先如何隐藏该元素?例如,如果您将其设置为
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 removedisplay: none
) and.addClass('show')
(to add your CSS class).尝试
用这个替换
Try replacing
with this one
使用
.addClass('hidden')
/.removeClass('hidden')
代替display: none
instead of
display: none
, use.addClass('hidden')
/.removeClass('hidden')