如何在锚标记内使用图像
<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>
这是我的代码。
我想要的行为是,如果我应该单击 div,它应该转到 xyz.php。如果我单击“打印地图”图像,则应该发生其他事情,并且锚标记的单击事件不应该起作用,这意味着它不应该转到 xyz.php 页面。只有那个 javascript 函数应该起作用。
我该怎么做?我不明白这个。因此,如果有人知道我如何才能做到这一点,请以这种方式告诉我。
<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. 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. only that javascript function should work.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将
移到
标记之外? (如果您希望它适用于键盘用户,您应该给它自己的
标记,并将 onclick 放在
上。)
另外,什么是
的要点?它可以让您做哪些现有
无法做到的事情?
编辑:如果您必须有div元素 - 当您可以设置锚元素的样式时,实际上您不应该需要它来进行样式设置 - 并且您希望图像位于同一个div内,那么为什么不拥有两个锚点,每个你想要点击的东西都有一个:
同样,有两个锚点可以让键盘用户使用。
(否则,为了让您的 HTML 保持原样,在我看来这是一个坏主意,您可以阅读事件
preventDefault()
、stopPropagation() 和/或
cancelBubble
。)顺便说一下,尝试在字符串中间包含变量是怎么回事:
"javascript:printStoreMap('+ e[0] +');"
(应该到处都是双引号,还是整个 HTML 块是您尚未发布的带有单引号的字符串赋值的一部分,或者...?)另请注意分配事件处理程序时不需要javascript
一词。Why don't you move the
<img>
outside the<a>
tag? (If you want it to work for keyboard users you should give it its own<a>
tag and put the onclick on the<a>
.)Also, what's the point of the
<div>
? What does it let you do that you can't do with the existing<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:
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/orcancelBubble
.)By the way, what's with trying to include a variable in the middle of a string here:
"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 wordjavascript
when assigning an event handler.