我有一行HTML:
<FONT size=4>Hello</FONT>
<FONT size=5>Hello</FONT>
我想用jQuery替换字体:
如果字体大小= 4,请替换为<h2 class="h2class">Hello</h2>
如果font size = 5,请替换为<h3 class="h3class">Hello</h3>
我一直在尝试使用replaceWith()函数:
$('font').replaceWith('<h2 class="h2class"></h2>');
但后来我丢失了文字,我也无法确定如何检查字体大小。
所以,有两个问题: 1.如何根据字体大小有条件地执行此操作 2.如何在不丢失原始文本的情况下执行此操作。
由于
答案 0 :(得分:2)
尝试
$('font[size="4"]').replaceWith(function(){
return $('<h2 />', {"class": "h2class"}).append($(this).contents())
});
$('font[size="5"]').replaceWith(function(){
return $('<h3 />', {"class": "h3class"}).append($(this).contents())
});
答案 1 :(得分:0)
$('font').wrapInner('<h2 class="h2class" />').children().unwrap();
答案 2 :(得分:0)
$('font').replaceWith(function(i, content) {
var tag = +$(this).attr('size')===5 ? 'h2' : 'h3';
return $('<'+tag+'/>', {
'class': tag+'class',
text: content
});
});