2010-05-24 138 views
3
<p> 
lorem ipsum lorem ipsums <font style="background-color:yellow">ipsum</font> ipsume lorem 
</p> 

如何与<abbr>更换<font>标签,因此放出来会是这样的。更换具有特定属性的元素,并将其值

<p> 
    lorem ipsum lorem ipsums <abbr style="background-color:yellow">ipsum</abbr> ipsume lorem 
    </p> 

回答

2

查找任何p中的所有字体标签,并用样式属性和复制文本替换为abbr。

$("p font").each(function() { 
    var font = $(this); 

    var abbr = $("<abbr>", { 
     style: font.attr("style"), 
     text: font.text() 
    }); 

    font.replaceWith(abbr); 
}); 
​ 
2

这将替换所有<font</font<abbr</abbr

var p = document.getElementsByTagName("p")[0]; // assume this is the first P in the document 
var p.innerHTML = p.innerHTML.replace(/(<|<\/)font/g, "$1abbr") 
1

也许你可以使用getAttributes plugin复制你的属性:

var attributes =$.getAttributes($(YOURTAG)); 
$(YOURTAG).replaceWith($('<abbr>' + YOURTAG.innerHTML + '</abbr>'); 
$(YOURTAG).attr(attributes); 

注:我很抱歉,但我没有测试这一个...

1

这应该做的招。

$('p').each(
    function(){ 
      $(this).html($(this).html().replace(/(<|<\/)font/g, "$1abbr")); 
    } 
); 
1

我来到这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head><title></title> 
<style type="text/css"><!-- 
--></style> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript"><!-- 
$(function(){ 
    $("font").replaceWith(function(){ 
     var font = $(this); 
     return $("<abbr></abbr>") 
      .attr("style", font.attr("style")) 
      .append(font.contents()); 
    }); 
}); 
//--></script> 
</head> 
<body> 

<p> 
lorem ipsum lorem ipsums <font style="background-color:yellow">ipsum</font> ipsume lorem 
</p> 

</body> 
</html> 

有可能是一个更直接的方式,但这似乎工作。

相关问题