在DOM中查找模式并使用jquery替换其内容 [英] Find a pattern in DOM and replace it's content using jquery
本文介绍了在DOM中查找模式并使用jquery替换其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个div,其中包含类似下面的元素
I have a div containing elements something like below
<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.com
,并替换为cab.global.com
,并在同一链接中附加?source = abc .
I need a way to find all abc.global.com
within anchor tag and replace with cab.global.com
and append ?source=abc in the same link.
那么我该如何遍历div下包含id ="clicker"的每个标记并替换锚点href值?
So how can I traverse through each a tag under div containing id="clicker" and replace the anchor href value?
推荐答案
此脚本应执行您想要的操作:
This script should do what you want:
$(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);
}
});
});
这篇关于在DOM中查找模式并使用jquery替换其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文