简体   繁体   English

如何获取在 function 中声明的变量的值

[英]how to get the value of a variable declared within a function

I'm a beginner in JS.我是JS的初学者。 In my personal project I want to use a datalist of colors to change the div color.在我的个人项目中,我想使用 colors 的数据列表来更改 div 颜色。

I managed to do it with select.我设法用 select 做到了。 But it doesn't work with datalist.但它不适用于数据列表。 So I decided to console log the value of the variable declared in the function but I get the error: x is not defined.所以我决定控制台记录在 function 中声明的变量的值,但我收到错误:x 未定义。 How to make it work?如何让它发挥作用? (It's probably a basic question) (这可能是一个基本问题)

 const selectSection = document.querySelector(".select-section"); function myFunction() { var x = document.getElementById("my-select").value; selectSection.style.backgroundColor = x; } console.log(x);
 <label for="my-select">Pick a color:</label> <select name="" id="my-select" value="Choose the background color" onChange="myFunction()"> <option value="default">default</option> <optgroup label="Warm Colors"> <option value="red">red</option> <option value="orange">orange</option> <option value="yellow">yellow</option> </optgroup> <optgroup label="Cool Colors"> <option value="green">green</option> <option value="blue">blue</option> <option value="purple">purple</option> </optgroup> </select>

Two options:两种选择:

1 1

const selectSection = document.querySelector(".select-section");

let x;

function myFunction() {
  x = document.getElementById("my-select").value;
  selectSection.style.backgroundColor = x;
}

myFunction()

console.log(x);

2 2

const selectSection = document.querySelector(".select-section");

function myFunction() {
  const x = document.getElementById("my-select").value;
  selectSection.style.backgroundColor = x;
  return x
}

const x = myFunction()
console.log(x);

Remember to never use var记住永远不要使用var

First please don't use the var keyword in your code read this article to know the reason首先请不要在代码中使用var关键字阅读本文了解原因

Second to solve this problem要解决这个问题

  1. try to declare your variables in the global scope:尝试在全局 scope 中声明您的变量:

    let x; // in global scope (outside the function)

  2. and assign a value inside the function:并在 function 中赋值:

    x = document.getElementById("my-select").value;

You can declare the variable outside the scope of the function, but you must make sure to only access that variable AFTER the function has completed, otherwise the code will run BEFORE the function executes and the variable will not have been altered yet. You can declare the variable outside the scope of the function, but you must make sure to only access that variable AFTER the function has completed, otherwise the code will run BEFORE the function executes and the variable will not have been altered yet.

 const selectSection = document.querySelector(".select-section"); let x = null; function myFunction() { x = document.getElementById("my-select").value; document.body.style.backgroundColor = x; // If this line executes, we know x has been modified, so // it's safe to try to access it outside of the function showX(); } function showX(){ console.log(x); }
 <label for="my-select">Pick a color:</label> <select name="" id="my-select" value="Choose the background color" onChange="myFunction()"> <option value="default">default</option> <optgroup label="Warm Colors"> <option value="red">red</option> <option value="orange">orange</option> <option value="yellow">yellow</option> </optgroup> <optgroup label="Cool Colors"> <option value="green">green</option> <option value="blue">blue</option> <option value="purple">purple</option> </optgroup> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM