如何在使用链接时排除元素 [英] How to exclude an element when using a link
问题描述
我现在有一个问题,我很好奇,如果有一种可能的方法来解决这个问题。我在 href
元素中包含了2 div
。问题是我想排除< p>
元素。有没有办法做到这一点,尽管它在 href
元素中?感谢。
I have an issue right now and i am curious if there is a possible way to solve this. I have 2 div
's enclosed in href
elements. The problem is that i want to exclude the <p>
element. Is there a way to do this despite it being inside the href
element? Thanks.
<a href= "sample.com">
<div class="1">
<p>Hello</p>
</div>
</a>
<a href= "test.com">
<div class="2">
<p>Hello</p>
</div>
</a>
推荐答案
是的,您可以但我不会主张。
Yes you can but I wouldn't advocate for it.
您可以使用CSS删除链接的外观:
You could use CSS to remove the appearance of a link:
a{
text-decoration: none;
}
p{
cursor: default;
color: #000;
}
然后您可以使用 preventDefault()
以防止 p
触发点击操作:
Then you could use preventDefault()
to prevent the p
from triggering the action on click:
$("p").click(function(e){
e.preventDefault();
});
你应该做的是添加另一个包装来包含你的元素,然后包装你的 div
与 a
类似:
What you really should do is add another wrapper to contain your elements and then wrap your div
with an a
like so:
<div class="wrapper">
<a href="http://www.google.com" target="_blank">
<div class="1"></div>
</a>
<p>Hello</p>
</div>
这篇关于如何在使用链接时排除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!