私はDIVを次のようにクリック可能にしています:
jQuery(".post").click(function(){
window.location=jQuery(this).find(".post-title a").attr("href");
return false;
});
ただし、リンクをライトボックスで開くことができるように、rel属性も追加したいと思います。追加してみまし.attr("rel=prettyphoto")
たが、うまくいかないようです。
私はDIVを次のようにクリック可能にしています:
jQuery(".post").click(function(){
window.location=jQuery(this).find(".post-title a").attr("href");
return false;
});
ただし、リンクをライトボックスで開くことができるように、rel属性も追加したいと思います。追加してみまし.attr("rel=prettyphoto")
たが、うまくいかないようです。
そのはず:
$("whatever").attr("rel", "prettyphoto");
ドキュメントノートとして:
attr( name ) is the getter
attr( name, value ) is the setter
クリックではなくページの読み込み時に属性を追加する必要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;
});
});
もし私があなたなら、私はそれをいくつかのステップに分けます、あなたは実際にその過程であなた自身の質問に答えるでしょう。
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番目の引数を記述すると、値が設定されます。