2012-09-13 54 views
1

我有一個包含元素類似於下面查找DOM的模式和替換它的內容使用jQuery

<input type="hidden" name="subscribe_message" value="Success"/> 
<div class="titleBlue">Alerts</div> 
<table cellpadding="2" cellspacing="0" width="100%"><tr><td align="LEFT" height="35"> 
<tr> 
<td><a style="cursor:hand;text-decoration:underline" target="_blank" href="https://abc.global.com/kb/solution/21/Pages/136776.aspx">ab1</a></td> 
</tr> 
<tr> 
<td><a style="cursor:hand;text-decoration:underline" target="_blank" href="https://abc.global.com/kb/solution/21/Pages/136775.aspx">ab2</a></td> 
</tr> 
<tr> 
<td><a style="cursor:hand;text-decoration:underline" target="_blank" href="https://abc.global.com/kb/solution/21/Pages/136774.aspx">ab3</a></td> 
</tr> 
<tr> 
<tr> 
<td><a href='#' onClick="javascript:window.open('https://my-prod.global.com/gmm2/kb/dlink.do?externalId=<a style="cursor:hand;text-decoration:underline" target="_blank" href="https://abc.global.com/kb/solution/20/Pages/157468.aspx">157468 </a></td>  
</tr> 
<tr> 
<td><a style="cursor:hand;text-decoration:underline" target="_blank" href="https://abc.global.com/kb/solution/20/Pages/147468.aspx">147468 </a></td> 
</tr> 
</table> 

我需要一種方法來找到錨標籤內的所有abc.global.comcab.global.com更換和追加一個div?源= ABC在同一鏈路。

那麼,怎樣才能通過我每次行程下的div包含ID =「點擊」標籤,更換錨href的值?

回答

1

這個腳本應該做你想要什麼:

$(document).ready(function(){ 
    // loop over all links inside the table 
    $("a", "#clicker").each(function(){ 
     var href = $(this).attr('href'); 

     if(href.indexOf("abc.global.com") >= 0){ 
      // replace domain 
      href = href.replace("abc.global.com", 'cab.global.com'); 
      // append source param 
      href += "?source=abc"; 
      $(this).attr('href', href); 
     } 
    }); 
});​ 

working fiddle

+0

人人都獻出了正確的答案,但因爲你即使給我一個解決方案,追加**?源= ABC **所以,接受你的解決方案的答案 – abi1964

+0

乾杯,祝你好運與您的項目 – Asciiom

+0

只是硬編碼的'?源= abc'一部分會失敗,其中源URL已經擁有了'?'或''#零件。如果您需要,我添加了功能。 –

1

試試這個:

$('div#clicker a').each(function(o){ 
    var _anchor = $(this).attr('href'); 

    if(_anchor.indexOf('abc.global.com')!=-1) { 
     _anchor = _anchor.replace('abc.global.com', 'cba.global.com'); 
     _anchor = modify(_anchor); 
     $(this).attr('href',_anchor); 
    } 
}); 

function modify(anchor) { 
    if(anchor.indexOf('?') != -1) { 
     anchor = anchor.replace(anchor.indexOf('?'), '?source=abc&'); 
    } else if(anchor.indexOf('#') != -1) { 
     anchor = anchor.replace(anchor.indexOf('#'), '?source=abc#'); 
    } else { 
     anchor += '?source=abc'; 
    } 

    return anchor; 
} 

希望它能幫助。

+0

改變。此前也一樣,它不會是一個錯誤,只是說了'replace'事情會在每一個匹配實體運行。無論如何,我的一個疏忽。 –

0

如果它的確定只是遍歷整個網站上的每個標籤可以使用

var links = document.getElementsByTagName("a"); 

獲得他們全部的清單。 然後,只需通過他們循環和變化的href酌情

for (var i = 0; i < links.length; i++) 
{ 
    if (links[i].href.search("abc.global.com") != -1) 
    { 
     links[i].href.replace("abc.global.com", "cab.global.com"); 
     links[i].href += "?source=abc"; 
    } 
} 

您可能需要暫時保存在HREF一個變量來提高性能,並可能增加一些安全檢查。

0

jsBin DEMO

$('a[href*=abc]').each(function(){ 
    return this.href = this.href.replace('abc', 'cab')+'?source=abc'; 
}); 
0

試試這個

$('a').each(function(a){ 
if ($(this).attr('href').toLowerCase().indexOf("abc.global.com") >= 0){ 

$(this).attr('href',$(this).attr('href').replace('abc.global.com','cab.global.com')); 
$(this).attr('href',$(this).attr('href') + '?source=abc') 
    } 
}); 

這裏是一個http://jsfiddle.net/MgsrV/