简体   繁体   English

如何将 css 属性的实际值传递给 javascript 变量

[英]How to pass the actual value of a css property to a javascript variable

So what I'm trying to do seems like it should be quite simple, but for some reason it's not working so maybe I'm missing something.所以我想要做的似乎应该很简单,但由于某种原因它不起作用所以也许我错过了一些东西。

Keep in mind the #element-title selector is rotating between 3 strings of text with another script.请记住,#element-title 选择器在 3 个文本字符串与另一个脚本之间旋转。

Here's the JS code这是JS代码

var elementTitles = new Array (
    "titleOne",
    "titleTwo",
    "titleThree"
);

var elementCurrentTitle = document.getElementById("element-title").innerHTML;

var elementOneColor = document.getElementById("element-1-id").style.color;
var elementTwoColor = document.getElementById("element-2-id").style.color;
var elementThreeColor = document.getElementById("element-3-id").style.color;

function rotateTitleColors(){
    if (elementCurrentTitle == elementTitles[0]){
        elementOneColor = "black";
        elementTwoColor = "grey";
        elementThreeColor = "grey";
    };
    else if (elementCurrentTitle == elementTitles[1]){
        elementOneColor = "grey";
        elementTwoColor = "black";
        elementThreeColor = "grey";
    };
    else if (elementCurrentTitle == elementTitles[2]){
        elementOneColor = "grey";
        elementTwoColor = "grey";
        elementThreeColor = "black";
    };
};

Basically, the idea is that depending on what the innerHTML of the #element-title selector is at the moment, it's the corresponding element (which is just text) that will be black and the other elements will be grey so the current element is looks highlighted.基本上,这个想法是,根据 #element-title 选择器的 innerHTML 目前是什么,对应的元素(只是文本)将是黑色的,其他元素将是灰色的,因此当前元素看起来突出显示。

For some reason it's not working and I've tried to do something very simple to test it.出于某种原因,它不起作用,我尝试做一些非常简单的事情来测试它。

var elementOneColor = document.getElementById("element-1-id").style.color;

function logElementColor(){
    console.log(elementOneColor);
};

And nothing shows up in the console.控制台中没有任何显示。 How am I supposed to get the actual value of the css "color" property so that I can pass it into a variable?我应该如何获得 css“颜色”属性的实际值,以便可以将其传递给变量?

Thanks in advance!提前致谢!

You cannot assign a property to a variable and expect that original property to change in JavaScript if the variable is reassigned.如果重新分配变量,您不能将属性分配给变量并期望原始属性在 JavaScript 中发生变化。

let a = { b: 'car', c: 'bike' }

let b = a.b

console.log(b)
// "car"

console.log(a)
// { b: "car", c: "bike" }

b = "cat"

console.log(b)
// "cat"

console.log(a)
// { b: "car", c: "bike" }

Notice how even if we changed the value of b , ab still remains unchanged.请注意,即使我们更改了b的值, ab仍然保持不变。 This is same for most of the programming language.这对于大多数编程语言都是一样的。

Solution解决方案

You could instead of assigning the color, directly assign the element to a variable and change it's color.您可以直接将元素分配给变量并更改其颜色,而不是分配颜色。

 var elementTitles = [ "titleOne", "titleTwo", "titleThree" ]; var elementCurrentTitle = document.getElementById("element-title").innerHTML; var elementOneColor = document.getElementById("element-1-id"); var elementTwoColor = document.getElementById("element-2-id"); var elementThreeColor = document.getElementById("element-3-id"); function rotateTitleColors() { if (elementCurrentTitle === elementTitles[0]) { elementOneColor.style.color = "red"; elementTwoColor.style.color = "green"; elementThreeColor.style.color = "green"; } else if (elementCurrentTitle === elementTitles[1]) { elementOneColor.style.color = "green"; elementTwoColor.style.color = "red"; elementThreeColor.style.color = "green"; } else if (elementCurrentTitle === elementTitles[2]) { elementOneColor.style.color = "green"; elementTwoColor.style.color = "green"; elementThreeColor.style.color = "red"; } }; rotateTitleColors() function logElementColor() { console.log(elementOneColor.style.color); }; logElementColor()
 <h2 id="element-title">titleOne</h2> <p id="element-1-id">Apple</p> <p id="element-2-id">Orange</p> <p id="element-3-id">Cat</p>

Mistakes错误

There were few mistakes you have you in your code.您的代码中几乎没有错误。

  1. Terminating if block with a semicolon, ;用分号终止 if 块, ; . . Blocks aren't supposed to be terminated.块不应该被终止。 If you do in an else if, It will throw an error saying "Unexpected else .如果你在 else if 中做,它会抛出一个错误,说“Unexpected else .

  2. Use double equals == only when you need Abstract Equality Comparison.只有在需要抽象相等比较时才使用双等号== In this case, you need Strict Equality Comparison, ie.在这种情况下,您需要严格的平等比较,即。 types aren't converted.类型不被转换。

// Abstract Equality Comparison
console.log('12' == 12) // true

// Strict Equality Comparison
console.log('12' === 12) // false

element.style.color only returns value if style was explicitly set on the element via style attribute. element.style.color 仅在样式通过样式属性显式设置在元素上时才返回值。 Otherwise it will be empty.否则它将是空的。 To get the "computed" style, you need to use window.getComputedStyle() see: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle要获得“计算”样式,您需要使用 window.getComputedStyle() 参见: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

Example:例子:

Html: Html:

<div>
    <span id="element-1-id" style="color:blue">abc</span>
    <span id="element-2-id">abc</span>
</div>

Javascript: Javascript:

var ey = document.getElementById("element-1-id");
console.log(ey.style.color) // will print blue
console.log(window.getComputedStyle(ey).color) // will print rgb(0, 0, 255) which is blue
ey = document.getElementById("element-2-id");
console.log(ey.style.color)  // will print empty string
console.log(window.getComputedStyle(ey).color) // will print rgb(x, y, z)    

depending what the style sheet rules applied to this element resulted what color assigning to it.取决于应用于此元素的样式表规则导致分配给它的颜色。

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

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