Javascript 用span标记替换标记

Javascript 用span标记替换标记,javascript,jquery,Javascript,Jquery,我想在下面提到的代码中使用javascript或jquery将标记替换为span标记 <a class="multi-choice-btn" id="abcd123"> <img class="x-panel-inline-icon feedback-icon " src="../images/choice_correct.png" id="pqrs123"> </a> 这一点应作如下更改 <span class="multi-choic

我想在下面提到的代码中使用javascript或jquery将标记替换为span标记

<a class="multi-choice-btn" id="abcd123">
     <img class="x-panel-inline-icon feedback-icon " src="../images/choice_correct.png" id="pqrs123">
</a>

这一点应作如下更改

<span class="multi-choice-btn" id="abcd123">
     <img class="x-panel-inline-icon feedback-icon " src="../images/choice_correct.png" id="pqrs123">
</span>

由于id是动态的,因此必须根据“多选btn”类别进行更换

请帮忙

var anchor = document.getElementById("abcd123"),
    span = document.createElement("span");

span.innerHTML = anchor.innerHTML;
span.className = anchor.className;
span.id = anchor.id;

anchor.parentNode.replaceChild(span,anchor);​

请参阅附件

var props=$(“.multi-choice btn”).prop(“属性”);
变量span=$(“”);
$.each(道具、函数(){
span.attr(this.name,this.value);
});
$(“.multi-choice btn”).children().unwrap().wrapAll(span);​
请参阅附件

var props=$(“.multi-choice btn”).prop(“属性”);
变量span=$(“”);
$.each(道具、函数(){
span.attr(this.name,this.value);
});
$(“.multi-choice btn”).children().unwrap().wrapAll(span);​

您可以执行以下操作:

$('a').contents().unwrap().wrap('<span></span>');​
// New type of the tag
var replacementTag = 'span';

// Replace all a tags with the type of replacementTag
$('a').each(function() {
    var outer = this.outerHTML;

    // Replace opening tag
    var regex = new RegExp('<' + this.tagName, 'i');
    var newTag = outer.replace(regex, '<' + replacementTag);

    // Replace closing tag
    regex = new RegExp('</' + this.tagName, 'i');
    newTag = newTag.replace(regex, '</' + replacementTag);

    $(this).replaceWith(newTag);
});
$('a').contents().unwrap().wrap(“”);​
演示:

如果要保留该属性,可以执行以下操作:

$('a').contents().unwrap().wrap('<span></span>');​
// New type of the tag
var replacementTag = 'span';

// Replace all a tags with the type of replacementTag
$('a').each(function() {
    var outer = this.outerHTML;

    // Replace opening tag
    var regex = new RegExp('<' + this.tagName, 'i');
    var newTag = outer.replace(regex, '<' + replacementTag);

    // Replace closing tag
    regex = new RegExp('</' + this.tagName, 'i');
    newTag = newTag.replace(regex, '</' + replacementTag);

    $(this).replaceWith(newTag);
});
//标记的新类型
var replacementTag='span';
//将所有a标记替换为replacementTag类型
$('a')。每个(函数(){
var outer=this.outerHTML;
//替换开头标签

var regex=new RegExp('您可以执行以下操作:

$('a').contents().unwrap().wrap('<span></span>');​
// New type of the tag
var replacementTag = 'span';

// Replace all a tags with the type of replacementTag
$('a').each(function() {
    var outer = this.outerHTML;

    // Replace opening tag
    var regex = new RegExp('<' + this.tagName, 'i');
    var newTag = outer.replace(regex, '<' + replacementTag);

    // Replace closing tag
    regex = new RegExp('</' + this.tagName, 'i');
    newTag = newTag.replace(regex, '</' + replacementTag);

    $(this).replaceWith(newTag);
});
$('a').contents().unwrap().wrap(“”);​
演示:

如果要保留该属性,可以执行以下操作:

$('a').contents().unwrap().wrap('<span></span>');​
// New type of the tag
var replacementTag = 'span';

// Replace all a tags with the type of replacementTag
$('a').each(function() {
    var outer = this.outerHTML;

    // Replace opening tag
    var regex = new RegExp('<' + this.tagName, 'i');
    var newTag = outer.replace(regex, '<' + replacementTag);

    // Replace closing tag
    regex = new RegExp('</' + this.tagName, 'i');
    newTag = newTag.replace(regex, '</' + replacementTag);

    $(this).replaceWith(newTag);
});
//标记的新类型
var replacementTag='span';
//将所有a标记替换为replacementTag类型
$('a')。每个(函数(){
var outer=this.outerHTML;
//替换开头标签

var regex=new RegExp('不是最短的,但可以工作:

$('.multi-choice-btn').replaceWith(function() {
    return $('<span>', {
        id: this.id,
        `class`: this.className,
        html: $(this).html()
    })
});​
$('.multi-choice btn').replaceWith(function(){
返回$(''){
id:this.id,
`class`:this.className,
html:$(this.html())
})
});​

请参见

不是最短的,而是有效的:

$('.multi-choice-btn').replaceWith(function() {
    return $('<span>', {
        id: this.id,
        `class`: this.className,
        html: $(this).html()
    })
});​
$('.multi-choice btn').replaceWith(function(){
返回$(''){
id:this.id,
`class`:this.className,
html:$(this.html())
})
});​

请参见尝试使用replaceWith和一个小的attrCopy逻辑。请参见下文

演示:

$('.multi-choice btn').replaceWith(function(){
var attrpcopy={};
for(var i=0,attrs=this.attributes,l=attrs.length;i
尝试使用replaceWith和一个小的attrCopy逻辑。请参见下文

演示:

$('.multi-choice btn').replaceWith(function(){
var attrpcopy={};
for(var i=0,attrs=this.attributes,l=attrs.length;i
它也有一套吗?我以为它只能返回。它也有一套吗?我以为它只能返回。出于好奇,你为什么需要这样做?(不关我的事,我知道)。出于好奇,可能会复制,你为什么需要这样做?(不关我的事,我知道)。可能的重复项将丢失
-标记中的所有属性。不要使用正则表达式来解析HTML。这很容易失败。浏览器是操作DOM的完美环境,无需返回字符串操作。您将丢失
-标记中的所有属性。不要使用正则表达式来解析解析HTML。这很容易失败。浏览器是操作DOM的完美环境,不需要回到字符串操作。