在DOM中查找一个模式并替换它's的内容使用jquery
Find a pattern in DOM and replace it's content using jquery
我有一个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>
我需要一种方法来找到锚标记中的所有abc.global.com
,并用cab.global.com
替换并附加?source=abc在同一链接中。
那么,我如何遍历div下包含id="clicker"的每个a标记并替换锚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);
}
});
});
工作小提琴
试试这个:
$( '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;
}
希望能有所帮助。
如果可以简单地循环浏览整个网站上的每个标签,则可以使用
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临时保存在变量中以提高性能,并可能添加一些安全检查。
试试这个
$('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/
jsBin DEMO
$('a[href*=abc]').each(function(){
return this.href = this.href.replace('abc', 'cab')+'?source=abc';
});
相关文章:
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 如何用jquery替换字符串中可能变化的字符
- 如何用JavaScript或jQuery替换一堆标签
- Javascript/jQuery替换tamil语言输入框中的最后一个单词
- 搜索并用jQuery替换整个HTML字符串
- 用jquery替换javascript语法
- 使用 jQuery 替换 HTML
- 使用 jquery 替换缺失的月份
- jquery替换不起作用
- Jquery 替换所有文本语法正则表达式中的变量
- 如何用jQuery替换HTML标签的一部分
- 如何使用 jQuery 替换文本中的 HREF 链接
- 用 jquery 替换图像和类
- 事件在替换为 JQuery 替换与()后未触发
- 使用 jquery 替换 href 的一部分
- 用jQuery替换Django中的图像
- jquery替换DIV数据,然后跟踪对它的点击
- jQuery替换挂起chrome
- jQuery替换为并追加
- 用jQuery替换同一结构中的不同单词