jquery 如果单击元素,则执行此操作,否则执行此操作
这现在让我很困惑。这是我想要实现的伪代码,我需要使用单击,因为它与我构建的触摸手势挂钩。
if body is clicked {
if click has touched .circle {
remove circle;
}
else {
create circle;
}
}
This is confusing me now. Here is the pseudo code for what im trying to acheive, i need to use click as that hooks with the touch gestures im building with.
if body is clicked {
if click has touched .circle {
remove circle;
}
else {
create circle;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:自从写下答案后,
delegate
已被on
,应该改用它。您可以(ab)使用发生的事件冒泡。您将点击侦听器附加到每个带有类
circle
的 div 上(可以使用 jQuery 的 委托 方法),一旦处理完该事件,取消冒泡,这样它就不会到达主体点击侦听器。并且,在身体监听器上,只需添加圆圈即可。
像这样的事情:
或者,更简单的方法,只需使用
event.target
检查点击事件的目标:注意:因为我创建了
circle
div 作为变量,所以只能是一个。如果你不想这样,你可以使用 jQuery.fn.clone,或者只是做你想要的需要当场。Edit: Since writing the answer below,
delegate
has been superseded byon
, which should be used instead.You can (ab)use the event bubbling that occurs. You attach the click listener to every div with a class
circle
that will ever be made (that can using jQuery's delegate method), and once you're done dealing with that event, cancel the bubbling, so it won't reach the body click listener.And, on the body listener, just append the circle.
Something like this:
Or, a simpler approach, simply check the target of the click event using
event.target
:Note: Because I created the
circle
div as a variable, there could only be one. If you don't want that, you can use jQuery.fn.clone, or just make what you need on the spot.尝试:
try:
您需要使用事件的
target
属性。You'll want to use the
target
property of the event.它不会完全满足您的要求,但会给您一些想法。
我想要这个有很多原因(更容易编码,符合人体工程学效率等):带有假按钮。
此外,您还可以使用可放置区域来创建和删除项目( http://docs.jquery.com/UI/可删除)。
It does not respond exactly to your request, but it will give you ideas.
I would like this for many reasons (easier to code, ergonomically efficient, etc): with a fake button.
Also, you can do droppable areas to create and delete items ( http://docs.jquery.com/UI/Droppable ).