2013-02-08 70 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