jQuery或any:只能使div的一部分可点击? [英] jQuery or any: Possible to make only a part of a div clickable?

查看:253
本文介绍了jQuery或any:只能使div的一部分可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以只作为一个可点击的一部分 - 像点击映射/ imagemap?可以用jQuery吗
提示:我不想编辑html(滥用200个单独的html文件的模板)。

解决方案

您的div被点击,事件处理程序接收到该事件,因此您可以测试点击的位置。



如果需要,您可能需要使用点击的元素位置和大小,使用 $(this).offset() $(this).width() $(本).height()。例如:

  $('mydivselector')点击(function(e){
if(e.pageY &$ $ $ $ $ $ $ * $(this).height()){
//底部被点击

} else {
/ / up的div被点击

}
});

如果要阻止默认操作和事件传播,请从事件处理程序返回false。 >

is it possible to make only a part of a div clickable -- like a clickmap/imagemap? May with jQuery? Hint: i don't want edit the html (misused template with 200 separate html files).

解决方案

When your div is clicked, the event handler receives the event, so you can test the position of the click.

If needed, you might want to use the clicked element position and size, using $(this).offset() , $(this).width() and $(this).height(). For example :

$('mydivselector').click(function(e) {
     if (e.pageY > $(this).offset().top + 0.5*$(this).height()) {
          // bottom was clicked

     } else {
          // up of the div was clicked

     }
});

If you want to prevent default action and event propagation, return false from the event handler.

这篇关于jQuery或any:只能使div的一部分可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆