2013-03-13 105 views
0

上一頁problam解決@use selectors in variable jquery在同一COE不想頁面跳轉

我必須做的功能像this 這是用一個大的jQuery代碼我不明白

,這裏是我的jsfiddle 我有做相同的功能,但點擊時頁面跳轉到該元素 我不想頁跳到我需要一些淡入和出

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

}); 

$(".close").click(function() { 
    $(this).closest("div.popupbox").hide(); 
}); 

和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錨點擊,但不想頁面跳轉/滾動到ID 我給在撥弄top:10000px測試這個問題,因爲我原來的頁面將其移動到在fiddle和特定元素

完整的代碼,我想this functionality

+0

好...和喲你的問題是?這看起來像對我的請求...而你甚至沒有問請... – henser 2013-03-13 11:31:39

+0

如何停止頁面跳轉我的網站 – 2013-03-13 11:32:26

回答

3

這一個嘗試:

$(".show").click(function(e) { 
    e.preventDefault(); 
    var link = $(this).attr('href'); //<----remove quotes in $('this') 
    $(link).fadeIn(); // <-------------use fadeIn() instead 
}); 

$(".close").click(function(e) { 
    e.preventDefault(); 
    $(this).closest("div.popupbox").hide(); 
}); 

和調整top:100000px的東西較少的人喜歡50px

+0

謝謝它的作品我把頂部:1000000px只用於測試 – 2013-03-13 11:50:03

1

使用preventDefault()

$(".show").click(function(e) { 
    e.preventDefault(); 
    var link = $(this).attr('href'); 
    $(link).show().animate(
    { 
     scrollTop: $(link).offset().top 
    },800);; 

}); 

$(".close").click(function(e) { 
    e.preventDefault(); 
    $(this).closest("div.popupbox").hide(); 
    $('body,html').animate(
    { 
     scrollTop: 0 
    }, 800); 
}); 
+0

它停止工作 – 2013-03-13 11:34:46

+0

其工作,但其停止跳躍向下滾動向下看你會看到圖像 – 2013-03-13 11:36:50

+0

它diden工作我測試它與頂部:100px;和top:10px; – 2013-03-13 11:39:38

1
$('#foo',function(event) { 
    event.preventDefault(); 
}); 
1

嘗試event.preventDefault()

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

}); 

$(".close").click(function(event) { 
    event.preventDefault(); 
    $(this).closest("div.popupbox").hide(); 
}); 

文檔http://api.jquery.com/event.preventDefault/

+0

你可以編輯它在小提琴它不適合我同樣的答案我也測試過之前 – 2013-03-13 11:37:42