如何重定向从DIV和绕过子锚点href [英] How to redirect from DIV and bypass child anchor href

查看:102
本文介绍了如何重定向从DIV和绕过子锚点href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请使用下面的代码获得帮助。我想给整个DIV添加一个onclick事件来重定向到一个url,但是我不能绕过包含在DIV内部的子锚中的href。:

即, (目标是重定向到google.com,点击图片或文本上的任意位置):

 < html> 
< div onclick =location.href ='http://www.google.com'>
< div>
< a href =http://en.wikipedia.org/>
< img height =200width =200src =http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png>
< / a>
测试
< / div>
< / div>
< / html>

当我使用本地URL如'file:/// E:/ Documents / Blog / Design / Tmp1.html'(我不知道为什么)。
谢谢。


更新:我在这个请求背后添加了这个想法:我需要这个用于我的博客的索引部分,Blogger使用相同的它用于个别帖子的例程。在索引中,我希望主Div中的每次点击都重定向到Post,但在帖子内部,图像中的点击必须转到图像的href。我的想法是,onclick事件只是在索引部分添加到DIV中。 解决方案

如果你是(不建议,但它是你的代码),你可以添加一个 return false; 给内部的锚点:

 < html> 
< div onclick =location.href ='http://www.google.com'>
< div>
< a href =http://en.wikipedia.org/onclick =return false;>
< img height =200width =200src =http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png>
< / a>
测试
< / div>
< / div>
< / html>

更新:根据您的请求的步骤2(这是一个可怕的,可怕的主意有很多原因,但它有效):

 < html> 
< div onclick =location.href ='http://www.google.com'>
< div>
< a href =http://en.wikipedia.org/onclick =if(this.parentElement.parentElement.onclick){return false;}id =demo>
< img height =200width =200src =http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png>
< / a>
测试
< / div>
< / div>
< / html>


Please I need your help with the code below. I want to add an onclick event to a whole DIV to redirect to an url but I cannot bypass the href included in a child anchor inside the DIV.:

i.e. (The goal is to redirect to google.com clicking anywhere on the image or the text):

<html>
  <div onclick="location.href='http://www.google.com'">
    <div>
      <a href="http://en.wikipedia.org/">
        <img height="200" width="200" src="http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png">
      </a>
      Test
    </div>
  </div>
</html>

This works fine when I use a local URL like 'file:///E:/Documents/Blog/Design/Tmp1.html' (I don't know why). Thank you.

Update: I'm adding the idea behind this request: I need this for the Index section of my blog that Blogger builds with the same routine that it uses for individual posts. In the Index I want that every click in the main Div redirects to the Post, but inside the post a click in the image must goes to the image's href. My idea was that the "onclick" event it's added dinamically to the DIV only for the Index section.

解决方案

If you're intent on doing this inline (not recommend but it's your code), you can add a return false; to the inner anchor:

<html>
  <div onclick="location.href='http://www.google.com'">
    <div>
      <a href="http://en.wikipedia.org/" onclick="return false;">
        <img height="200" width="200" src="http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png">
      </a>
      Test
    </div>
  </div>
</html>

Update: based on step 2 of your request (this is a terrible, terrible idea for a number of reasons but it works):

<html>
  <div onclick="location.href='http://www.google.com'">
    <div>
      <a href="http://en.wikipedia.org/" onclick="if ( this.parentElement.parentElement.onclick ) { return false; }" id="demo">
        <img height="200" width="200" src="http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png">
      </a>
      Test
    </div>
  </div>
</html>

这篇关于如何重定向从DIV和绕过子锚点href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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