我試圖在標題中放置一個鏈接,該鏈接基本上是選定的錨點href,並帶有前綴。我怎麼能做到以下幾點?我希望能夠在選定的錨元素上獲得this.href。Fancybox中的目標href
$(".gallery-module > li > a").fancybox(
{
title : '<a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi'
});
我試圖在標題中放置一個鏈接,該鏈接基本上是選定的錨點href,並帶有前綴。我怎麼能做到以下幾點?我希望能夠在選定的錨元素上獲得this.href。Fancybox中的目標href
$(".gallery-module > li > a").fancybox(
{
title : '<a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi'
});
當你綁定的fancybox:
$(".gallery-module > li > a").fancybox({
title: '<a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi'
});
this
不匹配<a>
元素,this
可能是window
所以this.href
不是<a>
的href
屬性,this.href
是(可能)window.href
這並不是你想要的標題。
如果你想在的fancybox使用來自鏈接信息,你就可以遍歷集合使用each
匹配的元素,並分別結合的fancybox每個<a>
:
$('.gallery-module > li > a').each(function() {
// 'this' will be the <a> in here
$(this).fancybox({
title: '<a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi'
});
});
裏面$('.gallery-module > li > a').each
回調, this
將是您要查找的<a>
。
我假設你正在使用fancybox v1.3.x,不是嗎?
使用該版本,除非它在回調函數內,否則不能直接引用this.href
。你的情況,你可以使用titleFormat
選項自定義標題像
$(".gallery-module > li > a").fancybox({
titleFormat: function(){
return '<div class="newTititleWrap"><a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi</div>'
}
});
注意,我裹在DIV
定製的冠軍類 = newTititleWrap
(或任何你喜歡的),你可以樣式使用css
後上。
UPDATE:我錯過了標籤fancybox-2
(我的壞)。對於的fancybox v2.0.6使用像beforeShow
回調選項:
$(".gallery-module > li > a").fancybox({
beforeShow : function() {
this.title = '<div class="newTititleWrap"><a href="'+ this.href + '-large">DOWNLOAD</a><br> High Resolution File, 300dpi</dv>';
}
}); // fancybox
我會讓張貼V1.3.4解決方案的情況下,它可以幫助別人。
你是對的,我錯過了標籤「fancybox-2」(我的壞)...將編輯答案。 – JFK
問題是怎麼回事,怎麼了? –
this.href返回undefined – JCraine
你有[現場演示代碼](http://jsfiddle.net/),我們可以看到和玩? –