如何使用jquery更改钩子,括号

时间:2015-01-21 16:46:53

标签: javascript jquery

如何在标签后的代码中更改括号 使用jQuery。我无法理解。

<section> 
<a href="/"></a>  
» 
<a class="dateBar" href="/"></a>  
» 
<a class="dateBar" href="/"></a>  
» 
<a class="dateBar" href="/"></a>  
» 
</ section> 

2 个答案:

答案 0 :(得分:0)

$('section').contents().each(function(inedx, el) {
    if(el.nodeType == 3 && el.textContent.indexOf('»') != -1){
        $(el).replaceWith('a');
    }
});

答案 1 :(得分:0)

html() jQuery方法可以接受一个可用于更改任何匹配元素的html的函数。返回值被替换。

然后,您可以将它与正则表达式替换(使用/ g全局选项)组合,它将非常快速地完成所有替换。

e.g。

$('section').html(function(){return $(this).html().replace(/»/g, "&gt;")});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/vh3etp6q/