2013-03-13 48 views
2

我必須做的功能就像this上的鏈接的點擊,它會顯示樣箱 這是用一個大的jQuery代碼在彈出的產品細節我不明白使用選擇

這裏是我的jsfiddle 我想給一些鏈接相同的類與不同的#標籤來顯示div ,我希望當我點擊鏈接它解決同樣的href值並顯示相應的結果,但它沒有工作 可以有人建議正確的方法 這裏是我的JS

$(".show").click(function() { 
    var link = $('this').attr('href'); 
    $(link).show(); 

}); 

和HTML

<a href="#popup" id="show" class="show">a</a> 
<a href="#popup1" id="show1" class="show">b</a> 
<a href="#popup2" id="show2" class="show">c</a> 

我想告訴#popup錨點擊fiddle

完整的代碼,我想this functionality

+0

您試圖顯示一個鏈接的href對象,這是行不通的。你喜歡什麼顯示 – 2013-03-13 08:23:12

回答

3

你應該叫$(this),不$('this')

  • $(this)包裝PS由this提到一個jQuery對象中的對象,
  • $('this')會遍歷所有的文件尋找HTML節點的標記this(很像$('div')會尋找HTML標記節點div);由於沒有,它會選擇一個空的節點列表。

工作小提琴:http://jsfiddle.net/Hg4zp/3/

(有也是一個錯字,調用的.hide().hide(")代替)

+0

或關閉報價,這也工作得很好(對於我在Chrome中)。 – 11684 2013-03-13 08:26:27

+0

感謝它的工作 和.hide()是一個錯誤,我也想嘗試一些感謝 – 2013-03-13 09:35:46

0

試試這個方法:

$(".show").click(function (e) { //<-----pass the event here 
    e.preventDefault(); //<--------------stop the default behavior of the link 
    var link = $(this).attr('href'); //<-remove the quotes $(this) 
    $(link).show(); 
}); 

$(".close").click(function() { 
    $(this).closest("div.popupbox").hide(); //<----use .hide(); not .hide("); 
}); 

你應該使用在這些情況下preventDefault()到停止點擊鏈接時發生的跳轉。