用span或div或p替换anchor标签 [英] replace the anchor tag with a span or div or p

查看:77
本文介绍了用span或div或p替换anchor标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项功能,一旦用户点击链接,就会发出ajax请求,如果发出请求,则该链接不再可以点击。



以下是我用来实现此目的:

  $('a [id ^ =rsvp _]')。click(function(e){
e.preventDefault();
$ .post(
$(this).data ('url'),
函数(数据){
$(this).replaceWith(function(){
alert(data);
return $(< span> ;+ data +< / span>);
});
}
);
});

此代码适用于以 rsvp _ 。一切似乎都有效,包括警报(数据)但是锚标记仍然存在。我只是想将锚标签替换为其他东西。



HTML代码段如下所示

 < a id = rsvp _ $ {event.id}href =#data-url =$ {createLink(action:'myaction',params:[eventid:event,userid:user])}>点击到RSVP< / a> 

更新



<请注意,我在页面上有多个这样的链接,即 rsvp_1,rsvp_2,rsvp_3 ..etc



我只想删除用户点击的链接上的锚标记。不是页面上的所有链接

解决方案

试试这个:

  $('a [id ^ =rsvp _]')。click(function(e){
e.preventDefault();
$ .ajax($ (this).data('url'),{
method:'POST',
context:this
})。done(function(data){
$(this ).replaceWith(< span>+ data +< / span>);
});
});


I have a functionality where once the user clicks a link, an ajax request is made and if the request is made then the link shouldn't be clickable anymore.

Here is what I'm using to accomplish this:

$('a[id^="rsvp_"]').click (function (e) {
    e.preventDefault();
    $.post(
        $(this).data('url'),
        function(data) {
            $(this).replaceWith(function(){
                alert (data);
                return $("<span>" + data + "</span>");
            });
        }
    );
})  ;

This code will apply to any id starting with rsvp_ . Everything seems to work including the alert(data) however the anchor tag still remains there. I simply want to replace the anchor tag to something else.

The HTML snippet looks like this

<a id="rsvp_${event.id}" href="#" data-url="${createLink(action: 'myaction', params: ["eventid": event, "userid": user])}">Click to RSVP</a>

Update

Please note that I have multiple links like this on the page i.e. rsvp_1, rsvp_2, rsvp_3 ..etc

I would just like to remove anchor tag on the link that the user clicked. not all the links on the page

解决方案

Try this:

$('a[id^="rsvp_"]').click(function (e) {
    e.preventDefault();
    $.ajax($(this).data('url'), {
        method: 'POST',
        context: this
    }).done(function(data) {
        $(this).replaceWith("<span>" + data + "</span>");
    });
});

这篇关于用span或div或p替换anchor标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆