如何在第二次单击元素时隐藏元素
我想在第一次单击元素后隐藏该元素(使用 jQuery),以便用户在此之后无法看到并单击该元素。
我怎样才能做到这一点?
谢谢。
I want to hide an element after it is first clicked (using jQuery), so that user can't see and click the element after that.
How can I do that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常简单:
上面的
"selector"
可以是任何有效的 jQuery 选择器 (例如,".click-to-hide"
使所有具有click-to-hide
类的元素都具有此行为)。隐藏元素是使用 jQueryhide
方法完成的(还有 < code>show(如果您想让元素稍后再次可见)。如果您在第一次隐藏元素后不打算对它们执行任何操作,您可能还需要考虑
删除
而不是隐藏
。更新:要在第二次点击时执行某些操作,您需要记住何时已经对某个元素进行了点击。如果事情不会变得更复杂,您可以使用一个类来实现此目的:
如果您想计算点击次数,您可以使用
data
函数来存储元素收到的点击量:Very simply:
"selector"
above would be any valid jQuery selector (e.g.".click-to-hide"
to make all elements with classclick-to-hide
have this behavior). Hiding the element is done using the jQueryhide
method (there is alsoshow
if you want to make the elements visible again later).If you do not intend to do anything at all with the elements after they are hidden for the first time, you might also want to consider
remove
instead ofhide
.Update: To do something on the second click, you need to remember when a click has already been made on an element. If it's not going to get more complicated than that, you could use a class for this purpose:
If you want to count the clicks, you can use the
data
function to store the amount of clicks the element has received:我无法发表评论,但我认为我使用的代码比原始答案更好。
它只是更加简单和压缩。
I can't comment, but I think the code I use works better than the original answer.
It's just much more simple and compressed.