4

我需要制作一个 div,以便当光标悬停在它上面时,我可以检测到它(使用 javascript),但我想制作它,以便您可以单击 div 到下面的元素。有没有办法做到这一点?

谢谢

4

2 回答 2

0

编辑 据我所知,通过我的快速搜索,我不相信你能够做到这一点,如果可以的话,我不会认为这不容易或非常实用。

老的

使用jQuery

$(document).ready(function(){
    $("body").on("hover", "div", function(){
         //do whatever you want on hover
    });

    $("body").on("click", "div .child-class", function(){
        //do whatever on click
    });
});

如果这不能回答您的问题并做您想做的事,请告诉我更具体的原因。

于 2012-12-27T21:01:02.167 回答
0

根据您的问题,有多种解决方案。我将从一些假设开始: 1. 你是页面的所有者(你知道那里发生了什么);2. 你知道被点击的元素下面是什么元素。

对于这种情况,请检查以下代码:

//function executed when you click on element with id: beneath
function clickOnBeneath() {
    alert("beneath click");
}

//function executed when you click on element with id: above
function clickOnAbove() {
    var beneathEl;
    beneathEl = document.getElementById("beneath");
    beneathEl.click();
}

//attach click event on element with id: above and beneath
function attachClickOnElements() {
    var aboveEl,
        beneathEl;
    aboveEl = document.getElementById("above");
    aboveEl.addEventListener("click", clickOnAbove, false);
    beneathEl = document.getElementById("beneath");
    beneathEl.addEventListener("click", clickOnBeneath, false);
}

attachClickOnElements();

还有工作示例:http: //jsfiddle.net/darkyndy/xRupb/

如果你不知道它下面是什么元素,那么我会尝试找到一些代码,因为我几年前写过这样的代码,作为起点,你可以检查HTML 元素上可用的getClientRects()函数(文档: https://developer.mozilla.org/en-US/docs/DOM/element.getClientRects )

于 2012-12-27T21:55:07.727 回答