2011-01-26 54 views
0

我試圖刪除或更改錨文本的部分使用jquery或javascript刪除錨文本的一部分

我的錨是這樣的:

<a href="http://something.com">Model 1234 motorbike</a> 

我想刪除錨文本的「摩托車」的一部分,以結束:

<a href="http://something.com">Model 1234</a> 

任何人都知道如何做到這一點?

感謝

更新:

的錨鏈接是從不同的網站來。所以我無法控制HTML。我只是試圖從發送的主播中刪除「摩托車」部分,所以它不會在我的網頁上顯示100次。

@Shiv庫馬爾:是的,「摩托車」始終是錨文本的最後部分。 @Progrog:腳本正在將可用模型發送到我的網站。錨文本的最後部分始終是「摩托車」,第一部分更改:型號1234,類型4321等。

但我會先給出代碼。

感謝大家的快速回復。

非常感謝。

埃裏克

+1

其全部以摩托車結束? – 2011-01-26 06:24:17

+0

jQuery IS JavaScript。 – amphetamachine 2011-01-26 06:25:07

回答

2

一)修改發送到瀏覽器的HTML,不要試圖修補它在客戶端上。如果用戶關閉JavaScript,該怎麼辦?

B)

// Get rid of the last word in the text for all anchors referencing 
// the domain something.com 
$('a[href*="something.com"]').html(function(oldHTML){ 
    return oldHTML.replace(/\s\S+$/,''); 
}); 

// Keep only "Model xxxx" 
$('a[href*="something.com"]').html(function(oldHTML){ 
    return oldHTML.replace(/^(Model \d+).+/, '$1'); 
}); 

編輯:從每一個環節專門刪除單詞 '摩托車':

$('a').html(function(oldHTML){ 
    return oldHTML.replace(' motorbike', ''); 
}); 
0

添加一個類名來過濾鏈接:

$(".with_motorbike_txt").each(function() { 
     var cur_txt = $(this).html(); 
     $(this).html(substring(0, cur_txt.indexOf('motorbike')); 
    });