在DOM中查找一个模式并替换它's的内容使用jquery

Find a pattern in DOM and replace it's content using jquery

本文关键字:jquery 替换 查找 DOM 模式 一个      更新时间:2023-09-26

我有一个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';
});