jQuery or any: Possible to make only a part of a div clickable?

空扰寡人 提交于 2019-12-19 04:57:07

问题


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).


回答1:


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.




回答2:


You can try to make a jQuery selector of all the element, and to slice for some

var selector = "#mydiv";
var numberOfElements = $(selector).find('*').length/2; //It's just an example

$(selector).find('*').slice(0, numberOfElements).click(function()  {
 [....]//do stuff
});



回答3:


You would probably need to make the whole div clickable but then by checking what coordinates was clicked figure out if you would like to handle the event or not.




回答4:


You can check the value of the offsetX and offsetY properties on the event object, and decide to do nothing if you don't like them.

Example.

Alternatively, if you need the click event to not fire at all, you could dynamically create additional transparent <div>s and place them partially in front of your existing element so that they obscure the parts which you don't want to respond to clicking.



来源:https://stackoverflow.com/questions/15339312/jquery-or-any-possible-to-make-only-a-part-of-a-div-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!