2013-02-08 45 views
1

我試圖使用.show()顯示幻燈片放映幻燈片中的div元素。jquery使用變量.show()div

我想要做的是使用我的導航欄上的按鈕的ID屬性,使幻燈片「幻燈片」非常像幻燈片放映。所以按鈕之一有一個「1」的ID。然後我使用Jquery將「#slide」添加到該ID並獲得「#slide1」。這對應於我希望展示的特定幻燈片。到目前爲止,我一直無法使用$ variable.show()顯示「slide」div。

我該怎麼處理這個錯誤?

這是我到目前爲止。

http://jsfiddle.net/greyoxide/RN2jC/

$(".button").click(function() { 
    $(".slide").hide(); 
    var content = $(this).attr('id'); 
    var fix = "'" + '#slide' + content + "'"; 
    $("#show").val(fix); 
    $(fix).show(); 
}); 

回答

0

擺脫單引號'的,所以改變:

var fix = "'" + '#slide' + content + "'"; // This translates to "'#slide1'" 

要:

var fix = '#slide' + content; // This translates to "#slide1" 

最終結果:

$(".button").click(function() { 
    $(".slide").hide(); 
    var content = $(this).attr('id'); 
    var fix = '#slide' + content; 
    $("#show").val("'" + fix + "'"); // If you still want to display single quotes 
    $(fix).show(); 
}); 

http://jsfiddle.net/RN2jC/9/

+0

感謝您的幫助和實例。我猜我需要引號才能以變量形式進行調用。 – greyoxide 2013-02-08 23:53:28

0

你不需要周圍的幻燈片ID的報價。

$(".button").click(function() { 
    $(".slide").hide(); 
    var content = $(this).attr('id'); 
    var fix = '#slide' + content; 
    $("#show").val(fix); 
    $(fix).show(); 
}); 

請參閱DEMO

0

fix string取消apostraphe:

var fix = "'" + '#slide' + content + "'";

應該是:

var fix ='#slide' + content;

0
$(".button").click(function() { 
    $(".slide").hide(); 
    $("#slide"+$(this).attr('id')).show(); 
}); 

jFiddle

+0

這比我原來的想法更加優雅。謝謝。 – greyoxide 2013-02-08 23:49:28