-2

好的,我无法逐字描述它,所以我将使用一个示例:

function f1() {
  document.write(vavariable)
};

function f2(){
  if(variablefot)
    document.body.style.color='red';
};

variablefot = false;
variablefotok = variablefot = true;
variable1 = 'text1' +variablefotok;

variable2 = 'text2';

vavariable = variable1

现在当我使用<script>f1();f2();</script>and vavariable = variable1 我应该得到红色 text1 但我得到红色 text1 + text 'true'

当我使用 vavariable2 时,我想得到没有体色的 text2 红色但我仍然得到 RED text2

我试图用函数来做到这一点,但它也不起作用。

请帮我让它工作。我已经失去了几个小时试图做到这一点。谢谢!

4

3 回答 3

1

Variablefotok 等于 true,所以这就是为什么写的是 true。

要更改主体颜色,只需使用 f2 功能,除了将颜色更改回默认值黑色。本质上,您可以将文本写入文档并每次根据变量更改颜色,或者您可以只将 HTML 代码写入文档,如<span style="color:red">text</span><span style="color:black">text</span>. 你也可以使用类来代替内联 CSS 声明。这适用于这种情况。

function write(){
    document.write(text);
    document.body.style.color = color;
}

variable1 = 'text1';
variable2 = 'text2';

使用该代码,您只需执行以下功能。如果您将所有文本存储在变量中,您也可以尽可能多地使用它,如图所示。

write(variable1, 'red');
write(variable2, 'black');
write(variable3, 'green');
write(variable4, 'blue');
write(variable5, 'brown');
write(variable6, 'yellow');
于 2013-11-02T20:56:43.270 回答
0

It's really unclear what you are trying to do, but I may be able to help anyway.

Here's what you have, more or less: http://jsfiddle.net/ySqGg/

I should get red text1 But I am getting red text1 + text 'true'

This happens because you are "adding" true to a string.

variablefotok = variablefot=true;
// variablefotok and variablefofot are now both set to true

variable1 = 'text1' +variablefotok;
// same as: variable1 = 'text1' + true;
// `true` is converted into a string so it can be combined with a string
// variable1 is now `text1true`

So why does that happen? Well lets look one line earlier:

variablefot = false;
variablefotok = variablefot = true;

This can be clarified as follows:

variablefot = false;
variablefotok = (variablefot = true);

So first you set variablefot to false. Then on the next line you set it to true. Assignment statements return the assigned value, and so you set variablefotok to true as well.

Then later f2 runs, and variablefot is now true, and you get red.

Did you mean to do a comparison instead?

variablefotok = (variablefot == true);

With this change, the text does not turn red, because variablefot remains false. http://jsfiddle.net/ySqGg/1/

But you rarely have to compare anything to true. You can simply use the variable, since it's true or false.

variablefotok = variablefot;

Which also does not turn anything red. See: http://jsfiddle.net/ySqGg/2/


This is confusing to answer because you are setting many variables, but only using one for anything usefult (variablefot in f2). If I were you I'd think about what variables you have and make sure you really need every one, then delete the others.

于 2013-11-02T21:02:22.437 回答
0

好吧,首先:

variablefotok = variablefot=true;

将首先将 true 分配给 variablefot,然后再分配给 variablefotok。因此两者都是真的。

variable1 = 'text1' +variablefotok;

由于 variablefotok 是true, variable1 现在将是'text1true'。因此变量最终将是'text1true'

function f2(){
    if(variablefot) 
        document.body.style.color='red';
};

如果 variablefot = true,将(调用时)将 body 的颜色更改为红色。既然这是真的,无论变量的值如何,它总是将身体的颜色变为红色。

于 2013-11-02T20:57:26.607 回答