Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改标记,但保留属性和内容——jQuery/Javascript_Javascript_Jquery_Dom Manipulation_Replacewith - Fatal编程技术网

更改标记,但保留属性和内容——jQuery/Javascript

更改标记,但保留属性和内容——jQuery/Javascript,javascript,jquery,dom-manipulation,replacewith,Javascript,Jquery,Dom Manipulation,Replacewith,改为 <a href="page.html" class="class1 class2" id="thisid">Text</a> 文本 我熟悉jQuery的replaceWith,但据我所知,它并没有保留属性/内容 注意:为什么p会有一个href?因为我需要在另一个事件中将p更改回a。尝试以下操作: <p href="page.html" class="class1 cl

改为

<a href="page.html" class="class1 class2" id="thisid">Text</a>

文本

我熟悉jQuery的
replaceWith
,但据我所知,它并没有保留属性/内容

注意:为什么p会有一个
href
?因为我需要在另一个事件中将
p
更改回
a

尝试以下操作:

<p href="page.html" class="class1 class2" id="thisid">Text</p>
var$a=$('a#thisid');
var ahref=$a.attr('href');
var aclass=$a.attr('class');
var-aid=$a.attr('id');
var atext=$a.text();
$a.replaceWith(“

“+atext+”

”);
做这个把戏的黑客

var $a = $('a#thisid');
var ahref = $a.attr('href');
var aclass = $a.attr('class');
var aid = $a.attr('id');
var atext = $a.text();
$a.replaceWith('<p href="'+ ahref +'" class="'+ aclass +'" id="'+ aid+'">'+ atext +'</p>');
var p=$('a').wrapAll('');
var a=$('div').map(函数(){
返回this.innerHTML;
}).get().join(“”);
$('div.replace').html(a.replace(/

这里有一个更通用的方法:

var p = $('a').wrapAll('<div class="replace"></div>');

var a = $('div').map(function(){
    return this.innerHTML;
}).get().join(' ');


$('div.replace').html(a.replace(/<a/g,'<p').replace(/a>/g,'p>'));
//标记的新类型
var replacementTag='p';
//将所有a标记替换为replacementTag类型
$('a')。每个(函数(){
var outer=this.outerHTML;
//替换开头标签

var regex=new RegExp(“以下是我用来替换jquery中html标记的方法:

// New type of the tag
var replacementTag = 'p';

// 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')。每个(函数(){
//创建新元素并从当前元素为其指定属性
var NewElement=$(“

”); $.each(this.attributes,function(i,attrib){ $(新元素).attr(attrib.name,attrib.value); }); //将当前元素替换为新元素并保留内容 $(this).replaceWith(函数(){ 返回$(NewElement).append($(this.contents()); }); });


我通常也会将它限制在一个特定的类中,例如
$('a.class1')。对于上面的示例,每个(function()

最好创建jQuery插件以备将来重用:

// Iterate over each element and replace the tag while maintaining attributes
$('a').each(function() {

  // Create a new element and assign it attributes from the current element
  var NewElement = $("<p />");
  $.each(this.attributes, function(i, attrib){
    $(NewElement).attr(attrib.name, attrib.value);
  });

  // Replace the current element with the new one and carry over the contents
  $(this).replaceWith(function () {
    return $(NewElement).append($(this).contents());
  });

});
用法:

(function (a) {
    a.fn.replaceTagName = function (f) {
        var g = [],
            h = this.length;
        while (h--) {
            var k = document.createElement(f),
                b = this[h],
                d = b.attributes;
            for (var c = d.length - 1; c >= 0; c--) {
                var j = d[c];
                k.setAttribute(j.name, j.value)
            }
            k.innerHTML = b.innerHTML;
            a(b).after(k).remove();
            g[h - 1] = k
        }
        return a(g)
    }
})(window.jQuery);

示例:

我将其制作成一个简单的javascript函数:

// Replace given object tag's name
$('a').replaceTagName("p");

为什么你不直接读取属性(href、class和id),然后在替换后重新分配它呢?+1好主意,你确定表达式中需要
g
标志吗?我想应该只进行一次替换,嗯?很好的解决方案,但当标签内部有另一个相等的标记时,我遇到了一个错误:
(转换为
),我修正了将“//Replace closing tag”部分改为:
regex=newregexp(“$”,“I”);newTag=newTag.Replace(regex.);
这是非常好的,Seth,我认为这是最干净的方法,也是最容易实现的方法。如果有错误,请将
g[h-1]=k
改为
g[h]=k
请注意,如果在单个对象上使用它,则需要像
a=a.replaceTangame(“p”)
function ReplaceTags(Original, Replace){
     var oarr = document.getElementsByTagName(Original);
     for(i=0; oarr.length < i; i++){
var html = oarr[i].outerHTML;
oarr[i].outerHTML = (html.replace(Original, Replace));
     }
}
function ReplaceTag(Original, Replace, customi){ 
// If customi = 0 the first appearance will get replaced
var i = customi;
    if(i == undefined)
     i=0;
var oarr = document.getElementsByTagName(Original);
var html = oarr[i].outerHTML;
oarr[i].outerHTML = (html.replace(Original, Replace));

}