简体   繁体   English

如何将 javascript 事件从一个元素传递到另一个元素?

[英]How do I pass javascript events from one element to another?

Setting up the stage:舞台搭建:

I have 2 layers one on top of the other.我有 2 层,一层在另一层之上。 The bottom layer contains links (simple images), The top layer contains advanced tooltip like hovers for the bottom layer.底层包含链接(简单图像),顶层包含高级工具提示,如底层的悬停。 These tooltips can be large (they can overlap onto other links easily, and almost always overlap the link they are tooltipping).这些工具提示可以很大(它们可以很容易地重叠到其他链接上,并且几乎总是与它们正在使用工具提示的链接重叠)。

My question:我的问题:

I'd like my mouseover events to occur on the bottom layer, and then on mouseover display the tooltip in the upper layer.我希望我的鼠标悬停事件发生在底层,然后在鼠标悬停时在上层显示工具提示。 This way as you move off of the bottom link the tooltip in the upper layer goes away, and the tooltip for the new link can show up.这样,当您离开底部链接时,上层中的工具提示就会消失,并且可以显示新链接的工具提示。

How do I take the events from the top layer and pass them to the layer below instead?如何从顶层获取事件并将它们传递到下面的层? So that the top layer is event transparent.这样顶层是事件透明的。

Example HTML:示例 HTML:

 jQuery(document).ready(function(){ jQuery('div.tile').click(function(){ jQuery('#log').html(this.id + " clicked"); return false; }); jQuery('div#top').click(function(){ jQuery('#log').html('Top clicked'); return false; }); });
 .tile { width: 100px; height: 100px; position: absolute; } .tile:hover, over:hover {border: 1px solid black;} .over { width: 100px; height: 100px; position: absolute; display:none} .stack, #sandwich { width: 400px; height: 400px; position: absolute; } #tile1 {top: 50px; left: 50px;} #tile2 {top: 75px; left: 10px;} #tile3 {top: 150px; left: 310px;} #tile4 {top: 250px; left: 250px;} #tile5 {top: 150px; left: 150px;} #over1 {top: 55px; left: 55px;} #over2 {top: 80px; left: 15px;} #over3 {top: 155px; left: 315px;} #over4 {top: 255px; left: 255px;} #over5 {top: 155px; left: 155px;}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <div id="sandwich"> <div class="stack" id="bottom"> <div class="tile" id="tile1">1</div> <div class="tile" id="tile2">2</div> <div class="tile" id="tile3">3</div> <div class="tile" id="tile4">4</div> <div class="tile" id="tile5">5</div> </div> <div class="stack" id="top"> <div class="over" id="over1">Tooltip for 1</div> <div class="over" id="over2">Tooltip for 2</div> <div class="over" id="over3">Tooltip for 3</div> <div class="over" id="over4">Tooltip for 4</div> <div class="over" id="over5">Tooltip for 5</div> </div> </div> <div id="log"></div>

With the example javascript I've verified that the events work like normal, and only top is clicked.通过示例javascript,我已经验证了事件正常工作,并且只单击了顶部。 But I basically want the "over" items to be event transparent.但我基本上希望“结束”项目是事件透明的。

Hope I understand the OP, but you can replicate the event on any selector after the original event has occurred: http://jsfiddle.net/Cr9Kt/1/希望我理解 OP,但您可以在原始事件发生后在任何选择器上复制事件: http : //jsfiddle.net/Cr9Kt/1/

In the linked sample, I take the event on the top layer and create a similar event fired on the bottom layer.在链接的示例中,我在顶层获取事件并在底层创建一个类似的事件。 You can take this further with any individual clicking of each element(as opposed to top and bottom as a whole).您可以通过单独单击每个元素(而不是整体的顶部和底部)来进一步执行此操作。

This is a borrowed idea from another question: Triggering a JavaScript click() event at specific coordinates这是从另一个问题借来的想法: Triggering a JavaScript click() event at specific坐标

For people stumbling on this nine years later (like I did) the best way to do this now is with the CSS pointer-events property... simply set it to 'none' for your element(s) and behold the magic.对于九年后(就像我一样)在这个问题上绊倒的人来说,现在最好的方法是使用 CSS 指针事件属性......只需将它的元素设置为“无”,看看魔法。 No JS required.不需要JS。

https://caniuse.com/#search=pointer-events https://caniuse.com/#search=pointer-events

I am not quite certain that I understand what you are asking for.我不太确定我理解你的要求。 It sound to me a bit like a tool tip.这听起来有点像工具提示。 Here is an example of how to do a tooltip this using jQuery, CSS and HTML.下面是一个如何使用 jQuery、CSS 和 HTML 执行工具提示的示例。

http://jsbin.com/ilali3/13/edit http://jsbin.com/ilali3/13/edit

Hope that gets you started.希望这能让你开始。 If you add some more details, or modify that jsbin with more details we can iterate a bit.如果您添加更多详细信息,或使用更多详细信息修改该 jsbin,我们可以进行一些迭代。

I updated the example a bit to include storing tool tip information into the html element itself using jQuery.我对示例进行了一些更新,以使用 jQuery 将工具提示信息存储到 html 元素本身中。 This is a bit cleaner.这更干净一些。

Bob鲍勃

This may seem overly complex and inelegant...it fakes your functionality.. but it works ;)这可能看起来过于复杂和不雅......它伪造了你的功能......但它有效;)

$(document).ready(function(){
    var tileData = new Array();

    // get coordinate data for each tile
    $('div.tile').each(function(){
        var tileInfo = {};   
        tileInfo.id = this.id;
        tileInfo.text = $(this).text();
        tileInfo.width = $(this).outerWidth();
        tileInfo.height = $(this).outerHeight();
        tileInfo.position = $(this).position();
        tileInfo.coords = {};
        tileInfo.coords.top = tileInfo.position.top;
        tileInfo.coords.right = tileInfo.position.left + tileInfo.width;
        tileInfo.coords.bottom = tileInfo.position.top + tileInfo.height;
        tileInfo.coords.left = tileInfo.position.left;

        tileData.push(tileInfo);
    });

    $('div.tile').click(function(){
        $('#log').html(this.id + " clicked");
        return false;
    })       

    $('div#top').click(function(event){
        $('#log').html('Top clicked');

        // try to find tile under your mouse click
        for(i=0; i<tileData.length;i++){
            if(
                event.pageX >= tileData[i].coords.left &&
                event.pageX <= tileData[i].coords.right &&
                event.pageY >= tileData[i].coords.top &&
                event.pageY <= tileData[i].coords.bottom
            )  {
                // found a tile! trigger its click event handler
                $('#' + tileData[i].id).click();
            }
        }  

        return false;
    });
});

Try it here: http://jsfiddle.net/neopreneur/vzq4z/1/在这里试试: http : //jsfiddle.net/neopreneur/vzq4z/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? javascript将元素从一个函数传递到另一个函数 - javascript pass element from one function to another 我如何将数据从一个类传递到另一类并更新元素的内容 - how do i pass data from one class to another and update an element's contents 如何将JavaScript变量从一个JSP页面传递到另一个 - How do I pass JavaScript variable from one JSP page to another 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何使用javascript动态地将一个svg元素居中 - How do I dynamically centre one svg element on another with javascript 如何按一个元素分组并在 Javascript 中找到另一个元素的平均值? - How do I group by one element and find the average of another in Javascript? 如何将参数传递给scala中的javascript元素? - How do i pass arguments to a javascript element from scala? RequireJS:如何将变量从一个文件传递到另一个文件? - RequireJS: How do I pass variables from one file to another? 如何将数据从一个组件实时传递到另一个组件? - How do i pass data from one component to another in realtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM