使用jquery将url替换为其他url

时间:2010-07-31 04:33:14

标签: jquery replace

如何使用jquery替换页面中具有不同URL的所有网址? 谢谢:))

3 个答案:

答案 0 :(得分:2)

发现这个:

//Create an object of all links
var links = $('a');
//Parse each item in links object
for (var a in links){
//This will allow the for iteration to give the actual link objects that are
//referred to with numeric indexes and not objects that jQuery appends
//Object 'a' should be a number
    if(a == parseInt(a)){
        //Variable b is now the object that is links[a];
        var b = links[a];
        //Variable c is now variable b cast to jQuery so I can use built in jQuery functions
        var c = $(b);
        //Variable temp now contains the href of that link
        var temp = c.attr('href');
        //This should filter out any anchors in the page or any links without an href
        if(temp != undefined){
            //This checks to see if they are inline links, mailto link, OR absolute link
            //This isn't perfect in the case that your link was 'mailsomething.php' or any non http link (ftp or other protocol)
            //The correct scenario here is to use regex but I didn't have the patience
            //or time to do so, so I didn't plus I knew my links didn't apply to these caveats
            var test = temp.substring(0,4);
            if(test != 'mail' && test != 'http' && test != '#'){
                //Now we prepend the abosulte url with the proper and add the relative file location
                c.attr('href','http://differentsubdomain.domain.com/'+temp);
            }
        }
    }
}

在此页面上:

Rewrite all links on a page...

答案 1 :(得分:1)

以下是一个例子:

$("a").attr("href","anotherurl.html");

答案 2 :(得分:0)

这将选择页面中的所有锚点: $( 'a')的

这将设置href属性: $( 'A')。ATTR( 'href' 属性, '')

我不确定那需要什么,等等。