简体   繁体   English

如何工作锚标签内的图像相似

[英]how to work Image clike inside anchor tag

<a href="xyz.php"><div id="some_id"><p>
<span class="storeName">name</span>
<span>phone number</span>
</p><img onclick="javascript:printStoreMap('+ e[0] +');" 
alt="Print Map"
src="/images/btn_print_map.png"></div></a>

This is my code. 这是我的代码。

The behavior i want is if i should click on div it should go to xyz.php. 我想要的行为是,如果我应单击div,则应转到xyz.php。 and if i will click on Print Map image than some thing else should happen and click event of anchor tag should not work means it should not go to the xyz.php page. 如果我将单击“打印地图”图像,则应该发生其他事情,并且锚标记的click事件不起作用,这意味着它不应转到xyz.php页面。 only that javascript function should work. 只有该javascript函数才能正常工作。

How can i do this? 我怎样才能做到这一点? I am not getting this. 我不明白这一点。 So if any body have idea how can i accomplish this then kindly tell that way. 因此,如果有人知道我该如何做到这一点,请以这种方式告诉我。

Why don't you move the <img> outside the <a> tag? 为什么不将<img>移到<a>标记之外? (If you want it to work for keyboard users you should give it its own <a> tag and put the onclick on the <a> .) (如果您希望它对键盘用户有效,则应给它自己的<a>标记,然后在<a><a> 。)

Also, what's the point of the <div> ? 另外, <div>什么? What does it let you do that you can't do with the existing <a> ? 使用现有的<a>不能做什么呢?

EDIT: If you must have the div element - and really you shouldn't need it for styling when you can style the anchor element - and you want the image inside the same div, then why not have two anchors, one for each thing you want to be able to click on: 编辑:如果您必须具有div元素-并且当您可以对锚元素进行样式设置时,确实不需要样式来进行样式设置-并且您希望图像位于同一个div中,那么为什么不使用两个锚,就您所需要的每一项而言希望能够单击:

<div id="some_id">
  <a href="xyz.php">
    <p>
      <span class="storeName">name</span>
      <span>phone number</span>
    </p>
  </a>
  <a onclick="printStoreMap('+ e[0] +'); return false;">
    <img alt="Print Map" src="/images/btn_print_map.png">
  </a>
</div>

Again, having two anchors makes it useable for keyboard users. 同样,具有两个锚点使其可用于键盘用户。

(Otherwise, to keep your HTML exactly as is which in my opinion is a bad idea you could read up on event preventDefault() , stopPropagation() , and/or cancelBubble .) (否则,要完全保持HTML的原样,我认为这是个坏主意,您可以阅读事件preventDefault()stopPropagation()和/或cancelBubble 。)

By the way, what's with trying to include a variable in the middle of a string here: "javascript:printStoreMap('+ e[0] +');" 顺便说一句,尝试在此处的字符串中间包含一个变量是什么: "javascript:printStoreMap('+ e[0] +');" (Should it have been double-quotes everywhere, or is the whole block of HTML part of some string assignment with single-quotes that you haven't posted, or...?) Note also that you don't need the word javascript when assigning an event handler. (应该是到处都是双引号,还是整个HTML块都是一些未分配但带有单引号的字符串分配的一部分,还是...?)请注意,您不需要javascript一词分配事件处理程序时。

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

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