我有一个包含如下元素的 div
<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>
我需要一种方法来找到 anchor 标记内的所有 abc.global.com
并替换为 cab.global.com
并附加 ?source=abc 在同一链接中。
那么如何遍历每个包含 id="clicker"的 div 下的 a 标签并替换 anchor href 值?
最佳答案
这个脚本应该做你想做的:
$(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);
}
});
});
关于javascript - 在 DOM 中查找模式并使用 jquery 替换它的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407290/