JQuery更改所有href title属性

时间:2014-04-01 22:31:03

标签: jquery loops attributes

我一直试图获取所有存在的链接,如果其标题属性包含'固定链接,则更改其标题属性。到目前为止,我有一小段代码来删除所有链接标题属性,但我还没有能够使用任何代码来检测是否永久链接到'存在于title属性中,如果是,则将其从标题中删除。 任何想法,帮助将不胜感激,谢谢。

$(document).ready(function($) {

//gets all links and removes 'Permalink to ' from title attribute if exists
var l = $('a');

//removes title attribute from all links
l.removeAttr('title');

});

这是我工作的{@ 3}}

的JSfiddle

4 个答案:

答案 0 :(得分:2)

您可以在选择器上执行外卡,以查找标题中包含'永久'

的所有标签
$(document).ready(function($) {
$('a[title*="Permalink"]').removeAttr('title'); 
 });

答案 1 :(得分:1)

例如:

l.each(function(){
  var $me = $(this);
  $me.attr('title', $me.attr('title').replace('Permalink to', '');
});

答案 2 :(得分:0)

希望这有帮助

$(document).ready(function($) {
    $( "a" ).each(function( index ) {
       $(this).removeAttr('title');
    });
});

答案 3 :(得分:0)

此代码将标题属性更改为'新标题'在每个链接元素中,哪个标题包含'永久链接到'。只需更换新标题'正确更改标题。

$(document).ready(function($) {
    $('a').each(function(){
        var $this = $(this);
        if (/Permalink to/.test($this.attr('title'))) {
            // replace title attribute with new/changed value
            $this.attr('title', 'new title');
        };
    });
});

JSFiddle

/Permalink to/RegExp,您可以根据自己的需要进行调整。例如,/^Permalink to /只匹配标题开头<#34;永久链接到&#34;,而/Permalink to/i将不区分大小写。