在<a> element have independent link

时间:2018-02-09 03:56:13

标签: javascript jquery html css

I have the following code:

HTML:

<a href="https://www.yahoo.com" target="_self">
  <div class="container">
    <p>
      Lorem Ipsum is simply dummy text of the printing and typesetting 
      industry. Lorem Ipsum has been the industry's standard dummy text ever since 
      the 1500s.<span>...more</span>
    </p>
</div>

CSS:

a {
  text-decoration: none;
}
.container {
  width: 220px;
  height: 220px
}
span {
  color: green !important;
  position: absolute;
  display: inline-block;
  z-index: 20;
}

JavaScript:

$('span').click(function(){
    location.href = "www.google.com";
});

My question is, How to make my span redirect to google.com when I click this span?

4 个答案:

答案 0 :(得分:2)

添加preventDefault解决了这个问题。

$('span').click(function(e){
    e.preventDefault();
    location.href = "https://www.google.com";
});

答案 1 :(得分:1)

在您的情况下,仅阻止浏览器对span元素e.preventDefault()进行默认操作就足够了:

$('span').click(function(e){
    e.preventDefault();
    location.href = "https://www.google.com/";
});

答案 2 :(得分:0)

我尝试了这个,它对我有用:

        $('span').click(function () {
            window.location.href = "http://www.google.com";
        });

答案 3 :(得分:0)

试试这个,

{{1}}