2

私はDIVを次のようにクリック可能にしています:

jQuery(".post").click(function(){
    window.location=jQuery(this).find(".post-title a").attr("href");
    return false;
});

ただし、リンクをライトボックスで開くことができるように、rel属性も追加したいと思います。追加してみまし.attr("rel=prettyphoto")たが、うまくいかないようです。

4

3 に答える 3

1

そのはず:

$("whatever").attr("rel", "prettyphoto");

ドキュメントノートとして:

attr( name ) is the getter
attr( name, value ) is the setter
于 2012-09-06T21:39:33.293 に答える
1

クリックではなくページの読み込み時に属性を追加する必要relがあります。ライトボックススクリプトを初期化する前に、必ず追加してください。とにかくページのリロードが行われ、属性が失われるため、クリック時に属性を設定しても意味がありません。

それで:

// Inside this block we're sure the DOM is loaded,
// so we can init our stuff
$(document).ready(function() {

    // Add rel to post title links
    $('.post .post-title a').attr('rel', 'lightbox');

    // Now init your lightbox script
    // (your init code here)

    // Now set the click handler (does not have to be last)
    $(".post").click(function(){
        window.location=$(this).find(".post-title a").attr("href");
        return false;
    });

});
于 2012-09-06T21:40:33.930 に答える
0

もし私があなたなら、私はそれをいくつかのステップに分けます、あなたは実際にその過程であなた自身の質問に答えるでしょう。

jQuery(".post").click(function(){

    // store the link in var
    var link = jQuery(this).find(".post-title a");

    // set the rel
    link.attr('rel','prettyphoto');

    // get the href
    var location = link.attr("href");

    window.location=location;
    return false;
});

また、.attr( attrName [, attrVal] )メソッドには2つの引数があることに注意してください。関数に2番目の引数を記述すると、値が設定されます。

于 2012-09-06T21:44:54.670 に答える