取出函数值(javascript)

时间:2016-11-22 18:06:32

标签: javascript function variables

我想在' .btn-random'中重复使用recipes_number1。单击按钮后,但我的代码不起作用... 我查了一下,发现recipes_number1不能带出...... 谁能救我? :(

Graph=ggplot(Preprobmelt,aes(x = age,y=Probability, colour=rel)) + 
geom_line(lwd=1.2) +
facet_grid(variable~ .,scales = "free")

Graph

2 个答案:

答案 0 :(得分:0)

我认为你在询问如何在点击发生后让Main_Dish_slide获得更新后的值:

var recipes_number1 ;
var Main_Dish_slide;

$('.btn-random').click(function(){
  recipes_number1 = Math.floor(Math.random() * 3);
  var photo_url = Appetizer_photos[ recipes_number1 ];
  var splitText = Appetizer[ recipes_number1 ];
  $('#random-photo').attr('src',photo_url);
  document.getElementById("list1").innerHTML= splitText; 
  Main_Dish_slide = Main_Dish_name[recipes_number1];
});



function showDivs(n) {
  var i;
  var x = document.getElementsByClassName(Main_Dish_slide);
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";
}

同样的概念,您需要重新分配Appertizer_slide引用的内容 - 这与您首次提供的代码之间的一个主要区别是,如果您打算立即调用showDivs,则需要为Appertizer_slide提供一个起始值:

var recipes_number1 ;
var Appertizer_slide = Appertizer_name[Math.floor(Math.random() * 3)];

$('.btn-random').click(function(){
  recipes_number1 = Math.floor(Math.random() * 3);
  var photo_url = Appetizer_photos[ recipes_number1 ];
  var splitText = Appetizer[ recipes_number1 ];
  $('#random-photo').attr('src',photo_url);
  document.getElementById("list1").innerHTML= splitText; 
  Appertizer_slide = Appertizer_name[recipes_number1];
});



var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName(Appertizer_slide);
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";
}

答案 1 :(得分:-1)

试试这个:

var recipes_number1, Main_Dish_slide;

$('.btn-random').click(function(){
  recipes_number1 = Math.floor(Math.random() * 3);
  var photo_url = Appetizer_photos[ recipes_number1 ];
  var splitText = Appetizer[ recipes_number1 ];
  $('#random-photo').attr('src',photo_url);
  document.getElementById("list1").innerHTML= splitText; 
  Main_Dish_slide = Main_Dish_name[recipes_number1];
});

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName(Main_Dish_slide);
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";
}