简体   繁体   English

在Anchor标记中嵌套可点击组件

[英]Nesting clickable components within an Anchor tag

Let's say I have some large DIV filled with content. 假设我有一些内容很大的DIV。

The content is dynamic, it could be anything, and it can include different types of clickable components - buttons, videos, links, etc. 内容是动态的,它可以是任何内容,它可以包括不同类型的可点击组件 - 按钮,视频,链接等。

I need the DIV itself to be clickable and route to some URL, but at the same time I need the different types of clickable components to be usable as well. 我需要DIV本身可以点击并路由到某个URL,但同时我需要不同类型的可点击组件也可以使用。 If you press any of them, it shouldn't invoke the routing at the DIV level. 如果按其中任何一个,则不应在DIV级别调用路由。

I've tried different approaches, none seem to work: 我尝试了不同的方法,似乎没有工作:

  1. Wrap the entire DIV with an A tag - any click inside the DIV will route to the URL, and none of the clickable components will be usable. 用A标签包裹整个DIV - DIV内的任何点击都将路由到URL,并且所有可点击组件都不可用。 Moreover, this prevents having other A tags inside that DIV as you can't nest A tags 此外,这可以防止在DIV中包含其他A标签,因为您无法嵌套A标签
  2. Add an onclick event to the DIV and route accordingly - this solves the A tag nesting problem, but again none of the clickable components is usable. 将一个onclick事件添加到DIV并相应地路由 - 这解决了A标记嵌套问题,但是再次没有可点击的组件可用。

The only reasonable solution that I can see is to handle click events on all the clickable components and to perform stopPropagation on the attached event so it won't bubble up to the DIV. 我能看到的唯一合理的解决方案是处理所有可点击组件上的点击事件,并对附加事件执行stopPropagation,这样它就不会冒泡到DIV。

But, this solution is just unfeasible for me as inside that DIV there could be components that are not under my control, like iframes with unknown content and functionality, video controls, etc. 但是,这个解决方案对我来说是不可行的,因为在DIV内部可能存在不受我控制的组件,例如具有未知内容和功能的iframe,视频控件等。

Is there a different approach I can use that I'm missing? 是否有一种我可以使用的不同方法,我错过了?

Update: 更新:

I went ahead to manage click events and stopPropegation on the different components. 我继续管理不同组件上的click事件和stopPropegation。

On A tags it works great. 在A标签上它很棒。

But when I have a video element with different control buttons, I tried to wrap it in a div and stopPropegation on that DIV, but it doesn't seem to work, not sure why. 但是当我有一个带有不同控制按钮的视频元素时,我试图将它包装在div和stopPropegation上的DIV上,但它似乎不起作用,不知道为什么。

Update 2: 更新2:

This is getting weirder by the minute... 这一刻变得更加怪异......

So I solved the video element clicks by adding both stopPropegation and preventDefault at the wrapping DIV. 所以我通过在包装DIV中添加stopPropegation和preventDefault解决了视频元素点击。 I have no idea why i needed to use preventDefault. 我不知道为什么我需要使用preventDefault。 Makes no sense. 没有意义。

Now I'm facing a different problem with another component. 现在我面临另一个组件的另一个问题。 I have a Bootstrap carousel, I'm wrapping it with a DIV and tried to stopPropegation on click events on that DIV, also tried to add preventDefault. 我有一个Bootstrap轮播,我用DIV包装它并尝试停止对该DIV上的点击事件进行调试,同时尝试添加preventDefault。 When I stopPropegation on that DIV the carousel buttons stop functioning!? 当我在那个DIV上停止调试时,旋转木马按钮停止运作!?

Some help and explanations would be greatly appreciated.. 一些帮助和解释将不胜感激..

Update 3: 更新3:

I figured I could use the defaultPrevented value on the event in order to figure out if to perform the routing on the main DIV element or not. 我想我可以在事件上使用defaultPrevented值,以确定是否在主DIV元素上执行路由。

I wrapped my main div with an A tag with a ui-sref attribute (forgot to mention this is an Angular app..), and binded to the ng-click event. 我用一个带有ui-sref属性的A标签包裹了我的主div(忘了提到这是一个Angular应用程序..),并绑定到ng-click事件。

But for some reason (seems to be related to ui-sref), jQuery's IsDefaultPrevented always returned true. 但由于某种原因(似乎与ui-sref有关),jQuery的IsDefaultPrevented总是返回true。

So I replaced the A tag with a DIV and performed the state change inside the controller after checking the IsDefaultPrevented value. 所以我用DIV替换了A标签,并在检查了IsDefaultPrevented值后在控制器内执行了状态更改。

So now it's all working. 所以现在一切正常。 But I'm not really sure why. 但我不确定为什么。

Look at my code; 看看我的代码; hopefully this 'll solve your issue; 希望这能解决你的问题;

The html is consider like this : html被认为是这样的:

<div id="div1" style="width:50px;height:50px;background-color:red">

         <a href="#">Hello</a>

</div>

The js code is like; js代码就像;

$(document).ready(function(){

    $("#div1").click(function(){
        alert("on div1");
    })

    $("#div1 a").click(function(e){
        e.stopPropagation();
        alert("on div1 a");
    })
})

Now if you click on "Hello" ; 现在,如果你点击“你好”; only "on div1 a" alert will show. 只有“在div1上”的警告才会显示。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM