替换<;A>;标记为<;部门>;使用jQuery
我有以下代码:替换<;A>;标记为<;部门>;使用jQuery,jquery,html,replace,Jquery,Html,Replace,我有以下代码: <div class="normal"> <div> <div>~Content 1~</div> </div> </div> <div class="normal replace"> <a href="#"> <div>~Content 2~</div> </a> </div&
<div class="normal">
<div>
<div>~Content 1~</div>
</div>
</div>
<div class="normal replace">
<a href="#">
<div>~Content 2~</div>
</a>
</div>
~Content 1~
使用jQuery,我想用普通的
标记替换
内的
标记,同时保留其内容
<div class="normal replace">
<div="result">
<div>~Content 2~</div>
</div>
</div>
~Content 2~
$('.normal>a')。替换为(函数(){
返回$(''){
html:this.innerHTML
});
});
给你,从这里开始
$('div.normal a').contents().unwrap().wrap(“”);
试试这个-
$('div.normal.replace a').find('div').unwrap().wrap("<div id='result'/>");
$('div.normal.replace a').find('div').unwrap().wrap(“”);
尝试以下方法:
// Select the 'a' tag to be replaced and call the replaceWith method
$('.normal a').replaceWith(function(){
// Execute a callback to generate contents of the replacement
// The $('<div>') part creates a div
return $('<div>', {
html: this.innerHTML // This takes the html of the 'a' tag and copies it to the new div
});
});
//选择要替换的'a'标记并调用replaceWith方法
$('.normal a').replaceWith(函数(){
//执行回调以生成替换的内容
//$('')部分创建一个div
返回$(''){
html:this.innerHTML//this获取“a”标记的html并将其复制到新的div
});
});
jQueryreplaceWith
方法获取该项的值,并将其替换为回调函数返回的值
有关更多信息,请参见使用以下工具
$('div.normal.replace a').replaceWith(“+$('div.normal.replace a').html()+”) 请单击下面我为您尝试的JSFIDLE示例
希望能有所帮助。这个问题一定有很多重复的地方……请用正确的格式写。我想你忘了提到id或类了。@vinothini这显然是一个例子,这真的重要吗!;-)@遗憾的是,Zenith只有更改div内容的解决方案,而不是在保留其内容的同时替换div。返回值应该是
return$('
还是return$('
)?您返回的确切内容是什么?我不理解您返回内容的结构。
$('div.normal.replace a').find('div').unwrap().wrap("<div id='result'/>");
// Select the 'a' tag to be replaced and call the replaceWith method
$('.normal a').replaceWith(function(){
// Execute a callback to generate contents of the replacement
// The $('<div>') part creates a div
return $('<div>', {
html: this.innerHTML // This takes the html of the 'a' tag and copies it to the new div
});
});