0

次のHTMLがあります。jQuery を実装して、各 readmore リンクに href 属性を追加したいと考えています。ここで readmore リンクを選択する方法を知りたいです。readmore リンクは動的に生成されます。各 readmore リンクに異なる値を指定したいのですが、方法はありますか? これを使ってみました。

アップデート

.content の最初の子の中の a タグを取得したい...取得方法は...???

jQuery('.content.c7:nth-child(1)').attr('href','www.google.com');

しかし、うまくいきませんでした。ここで何が間違っていますか?私を助けてください。

 <div class="content c7">
        <div class="products">
            <div id="post_content">
                Hai
            </div>
            <div class="end readmore">
                <a >Read More</a>
            </div>
        </div>
        <div class="products">
            <div id="post_content">
                hello
            </div>
            <div class="end readmore">
                <a>Read More</a>
            </div>
        </div>
        <div class="products">
            <div id="post_content">
                how are you
            </div>
            <a >Read More</a>
        </div>
        <div class="products">
            <div id="post_content">
                I am fine
            </div>
            <div class="end readmore">
                <a>Read More</a>
            </div>
        </div>
    </div>
4

4 に答える 4

1

あなたはこれを行うことができます:

$('.content.c7 .readmore a').attr('href','www.google.com');

フィドルのデモ

アップデート

$('.content.c7 .readmore a').each(function (index) {
    if (index == 0) {
        // Set href for the first element
        $(this).attr('href', 'www.google.com');
    } else if (index == 1) {
        // Set href for the second element
        $(this).attr('href', 'www.yourlink.com');
    }
    // Similarly set href for other elements individually
});

フィドルのデモ

アップデート

$('.content.c7 .readmore:eq(0) a').attr('href','www.google.com');

フィドルのデモ

于 2013-07-22T13:51:03.300 に答える
0

新しいコード作業デモhttp://jsfiddle.net/cse_tushar/vZnUU/2/

$(document).ready(function () {
    var links = ["www.google.com", "www.yahoo.com", "www.fb.com","www.hotmail.com"];
    i =0;
    $('.products a').each(function () {
        $(this).attr('href', links[i]);
        i++;
    });
});

ワーキングデモhttp://jsfiddle.net/cse_tushar/vZnUU/

$(document).ready(function () {
    $('.products div a').each(function () {
        $(this).attr('href','www.google.com');
    });
});

テキストが「続きを読む」の場合にのみリンクを変更したい場合

ワーキングデモhttp://jsfiddle.net/cse_tushar/vZnUU/1

$(document).ready(function () {
    $('.products div a').each(function () {
        if($(this).text() == 'Read More')
        $(this).attr('href','www.google.com');
    });
});
于 2013-07-22T13:56:04.430 に答える
0

すべての「続きを読む」リンクについては、次のようなものを選択します。

var rmlinks = jQuery('.content.c7 .readmore a');

それらをすべて同じものに設定したい場合は、次の方法で行うことができます。

rmlinks.attr('href', 'http://example.com');

他の場所にある hrefs のリストに基づいて 1 つずつヒットする場合は、次のようにします。

for ( i = 0; i < rmlinks.length; ++i )
{
  var newhref = "???";  // up to you
  $(rmlinks[i]).attr('href', newhref);
}
于 2013-07-22T13:52:04.350 に答える