Jquery 如何将不同的链接页分配给不同的链接?

Jquery 如何将不同的链接页分配给不同的链接?,jquery,Jquery,假设我在页面中有四个链接,第一个链接:“href”值应为“one.html”,第二个链接:“href”值应为“two.html”,第三个链接:“href”值应为“three.html”,第四个链接:“href”值应为“four.html” <a href="/mysite/">one</a>| <a href="/mysite/">two</a>| <a href="/mysite/">three</a>| <a hr

假设我在页面中有四个链接,第一个链接:“href”值应为“one.html”,第二个链接:“href”值应为“two.html”,第三个链接:“href”值应为“three.html”,第四个链接:“href”值应为“four.html”

<a href="/mysite/">one</a>|
<a href="/mysite/">two</a>|
<a href="/mysite/">three</a>|
<a href="/mysite/">four</a>
|
|
|
因此,结果将是:

<a href="/mysite/one.html">one</a>|
<a href="/mysite/two.html">two</a>|
<a href="/mysite/three.html">three</a>|
<a href="/mysite/four.html">four</a>
|
|
|

您可以在数组中指定不同的值,并使用如下方法:

$('a').attr('href',function(i,val){
    var arr = ['one','two','three','four'];
    return val + arr[i] + ".html";
});

而您尝试过的不起作用的代码是where?没有什么区别,但是不迭代数组4次不是更好吗?
$(function() {
    $("a").attr('href', function(i, old) {
        return old + $(this).text() + ".html";
    });
});