2010-05-24 140 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> 

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

相關問題