要应用的功能,整个标题的innerHTML
,其中包括锚的href
属性。这应该为你的提琴例如工作:
然而,它只会在里面的标题联系工作,我不知道这是你在找什么。如果你想要的东西,一定元素中的任何内容工程(含任何级别标记嵌套的),那么你就需要一个递归方法。这里有一个想法,这基本上是普通的JavaScript,因为jQuery的不提供一种方式来针对DOM文本节点:
$.fn.ligature = function(str, lig) {
return this.each(function() {
recursiveLigatures(this, lig);
});
function recursiveLigatures(el, lig) {
if(el.childNodes.length) {
for(var i=0, len=el.childNodes.length; i<len; i++) {
if(el.childNodes[i].childNodes.length > 0) {
recursiveLigatures(el.childNodes[i], lig);
} else {
el.childNodes[i].nodeValue = htmlDecode(el.childNodes[i].nodeValue.replace(new RegExp(str, 'g'), lig));
}
}
} else {
el.nodeValue = htmlDecode(el.nodeValue.replace(new RegExp(str, 'g'), lig));
}
}
// http://stackoverflow.com/a/1912522/825789
function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}
};
// call this from the document.ready handler
$(function(){
$('h3').ligature('ffi', 'ffi')
.ligature('ffl', 'ffl')
.ligature('ff', 'ff')
.ligature('fi', 'fi')
.ligature('fl', 'fl');
});
,应该在内容像这样工作:
<h3>
mixed ffi content
<span>this is another tag ffi <span>(and this is nested ffi</span></span>
<a href="/news/here-is-a-url-with-ffi-ligature">Here is a ffi ligature</a>
</h3>
http://jsfiddle.net/JjLZR/
,现在我知道一个连字是什么! –