How to exclude an element when using a link

I have a problem right now and am curious if there is a way to fix this problem. I have 2 div

enclosed in elements href

. The problem is I want to exclude an element <p>

. Is there a way to do this despite being inside an element href

? Thank.

<a href= "sample.com">
  <div class="1">
    <p>Hello</p>
   </div>
</a>
<a href= "test.com">
  <div class="2">
    <p>Hello</p>
  </div>
</a>

      

-1


source to share


2 answers


Yes, you can, but I would not defend him.

You can use CSS to remove the appearance of the link:

a{
  text-decoration: none;
}

p{
  cursor: default;
  color: #000;
}

      

Then you can use preventDefault()

to prevent p

the action from triggering on click:



$("p").click(function(e){
   e.preventDefault();
});

      

FIDDLE

What you really need to do is add another wrapper to contain your elements, then wrap div

with a

like this:

<div class="wrapper">
  <a href="http://www.google.com" target="_blank">
     <div class="1"></div>
  </a>
  <p>Hello</p>
</div>

      

0


source


$(function(){
  $('p').click(function(){
    alert($(this).attr('href'));
    // or alert($(this).hash();
  });
});

      



0


source







All Articles