dravpuso44681
2016-08-31 12:34
浏览 87
已采纳

jQuery:禁用锚点并使用相同的href创建一个新锚点

I'm using a Wordpress plugin for loading Facebook events into a web page. There is a big anchor with an event div in it. I want a small anchor at the bottom of every fb-event div instead.

I need to:

Disable the first anchor without removing it (removing it would also remove the entire div).

Create a new anchor at the bottom of the fb-event div.

Pass the href from the first anchor to the second.

I feel like I'm pretty close but nothing happens, this is my code, first html, then jQuery. Thanks in advance!

<div class='fb-event'>
<a class="fb-event-anchor" href="http://www.facebook.com/event.php?eid=1165884803462190"  target="_blank">
<div class="fb-event-desc"><img src=https://scontent.xx.fbcdn.net/t31.0-8/s720x720/13920278_1335410149821833_1686522516711277604_o.jpg />
<div class='fb-event-title'>Spiritueel weekend - Een stapje verder</div>
<div class='fb-event-time'>10 september 2016 &#183; 10:00 -<br>11 september 2016 &#183; 17:00</div>
<div class='fb-event-location'>Balance in Life</div>
<div class='fb-event-description'>Lots
</div>
</div>
</a>
</div>

jQuery:

var fblink = $('.fb-event-anchor').attr('href');

$('.fb-event-anchor').click(function(){
                  return false; // prevents default action
});
$('.fb-event-description').append('<a class="fb-event-anchor-new" href="' . [fblink] . '"></a>');
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushu2699 2016-08-31 12:42
    已采纳

    Try with below code

    $(function () {
        $('.fb-event-anchor').on("click", function (e) {
            e.preventDefault();
        });
    
    var fblink = $('.fb-event-anchor').attr("href");
        $( ".fb-event" ).append( '<a class="fb-event-anchor-new" href='+ fblink +'>NEW LINK</a>' );
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushi8186 2016-08-31 12:42

    you want to do like this ?

    var fblink = $('.fb-event-anchor').attr('href');
    
        $('.fb-event-anchor').click(function () {
            return false; // prevents default action
        });
        $('.fb-event-description').append('<a class="fb-event-anchor-new" href="'+fblink+'">fbanchor</a>');
    
    评论
    解决 无用
    打赏 举报
  • donglin5770 2016-08-31 13:36

    Both solutions succesfully create a new link but neither makes the first link unclickable. I countered that last part with css:

    .fb-event-anchor {
       pointer-events: none;
       cursor: default;
    }
    

    Thanks both, Pratik was first so I'll accept his answer.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题