替换标签并保留属性 [英] Replace tag and keep attributes

查看:42
本文介绍了替换标签并保留属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
更改标签名称,但保留所有属性

Possible Duplicate:
Change the tag name but keep all the attributes

是否可以将<a>标签替换为<span>标签并保留其所有属性?

Is it possible to replace an <a> tag with a <span> tag and keep all of it's attributes?

我有这个HTML,当有人点击编辑时,我想将所有编辑链接转换为span的

I have this HTML, and when someone clicks on Edit I want to convert all edit links to span's

<a href="javascript:void(0);" class="edit" data-id="123">Edit</a>

用户完成编辑后(通过按保存取消),然后我想将所有span标签转换回链接.

Once the user is done editing (by either pressing Save or Cancel) I then want to convert all the span tags back into links.

$(".edit").live("click", function(){
    $(".edit").addClass("inactive-link");
});

推荐答案

正如FAngel所指出的,在这种情况下,样式更改要简单得多.在我的示例中,我将继续使用事件委托而不是.live,因为在以后的jQuery版本中将删除.live:

As FAngel indicates, style changes are much simpler in this case. I'm going to go ahead and use event delegation instead of .live in my example because .live will be removed in later jQuery versions:

$(document).on('click', '.edit', function (e) {
   //add inactive class to *this* link
   $(this).addClass('inactive-link');

   //stop default link behavior, i.e. opening a new page
   e.preventDefault();
});

这篇关于替换标签并保留属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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