简体   繁体   English

更改变量值 onclick

[英]Change variable value onclick

I am new to JavaScript and I need some help.我是 JavaScript 新手,需要一些帮助。 I am making a website in shades of orange, in the website i have two a buttons (a).我正在制作一个橙色阴影的网站,在网站上我有两个按钮 (a)。 and i have two divs that can be shown by clicking on the a button.我有两个 div,可以通过单击 a 按钮来显示。 i have three more divs colored in red, blue and orange, by clicking them the page change the background, the header, buttons and the divs to shades of blue, red or orange.我还有三个红色、蓝色和橙色的 div,通过单击它们,页面将背景、标题、按钮和 div 更改为蓝色、红色或橙色的阴影。 when i click a button (a) his color turns to white.当我单击按钮 (a) 时,他的颜色变为白色。 when i push the other button he turns white and the first turn orange (thats good).当我按下另一个按钮时,他变成白色,第一个变成橙色(很好)。 but if i turn the page in to shades of blue, click on the first button and then on the second one the first turns back to orange and not blue.但是如果我将页面变成蓝色阴影,请单击第一个按钮,然后单击第二个按钮,第一个按钮会变回橙色而不是蓝色。 how can i make it right?我怎样才能做到正确?

i've tried make an if statement and a variable inside a function.我试过在函数中创建一个 if 语句和一个变量。 when i make it blue in the function i've wrote var shades="blue" (and the same for red and orange).当我在函数中将其设为蓝色时,我已编写了 var shades="blue"(红色和橙色也是如此)。 then in the a button function i've wrote color background to white and then turn the ather button to an if statement (if shades="blue" turn background to blue) and then another statement (if shades="red" turn background to red) and the same for orange.然后在按钮函数中,我将颜色背景写入白色,然后将 ather 按钮变为 if 语句(如果 shades="blue" 将背景变为蓝色),然后是另一个语句(如果 shades="red" 将背景变为红色)和橙色相同。 but it didn't work because the variable value changed only in the function and then stoped.但它不起作用,因为变量值仅在函数中更改然后停止。 how can i change variables value by clicking an a button?如何通过单击按钮更改变量值?

Thank you.谢谢你。

exemple:例子:

<header>
    <ul id="headerul">
        <li><a onclick="page1()" href="#" id="a" >page1</a></li>
        <li><a onclick="page2()" href="#" id="a1">page2</a></li>

        <div id="blue" onclick="colorB()"></div>
        <div id="red" onclick="colorR()"></div>
        <div id="orange" onclick="colorO()></div>
    </ul>
</header>

<script>
        function colorB(){
        document.getElementById('body').style.cssText =
        'background-color:a7a7ff;'
        document.getElementById('headerul').style.cssText =
        'background-color:7777ff;'
        document.getElementById('a').style.cssText =
        'background-color:#a7a7ff;'
        document.getElementById('a1').style.cssText =
        'background-color:#a7a7ff;'
        var body = 'blue'
    }
    function colorR(){
        document.getElementById('body').style.cssText =
        'background-color:ff5252;'
        document.getElementById('headerul').style.cssText =
        'background-color:ff2222;'
        document.getElementById('a').style.cssText =
        'background-color:#ff5252;'
        document.getElementById('a1').style.cssText =
        'background-color:#ff5252;'
        var body = 'red'
    }
    function colorO(){
        document.getElementById('body').style.cssText =
        'background-color:#ffc5a0;'
        document.getElementById('headerul').style.cssText =
        'background-color:#ff8027;'
        document.getElementById('a').style.cssText =
        'background-color:#ffc5a0;'
        document.getElementById('a1').style.cssText =
        'background-color:#ffc5a0;'
        var body = 'orange'
    }
    function page1(){
        document.getElementById('a1').style.cssText =
        'background-color:white;'
              if (body=="blue") {
              document.getElementById('a').style.cssText =
             'background-color:#a7a7ff;'
        };
              if (body=="orange") {
              document.getElementById('a').style.cssText =
              'background-color:#ffc5a0;'
        };
    }
    function page2(){
        document.getElementById('a').style.cssText =
        'background-color:white;'
              if (body=="blue") {
              document.getElementById('a1').style.cssText =
             'background-color:#a7a7ff;'
        };
              if (body=="orange") {
              document.getElementById('a1').style.cssText =
              'background-color:#ffc5a0;'
        };
    }
</script>

This is nearly impossible without seeing your code but at a guess this may help you in some way:如果没有看到您的代码,这几乎是不可能的,但猜测这可能会以某种方式帮助您:

https://jsfiddle.net/1xv6qjpx/ https://jsfiddle.net/1xv6qjpx/

I would add this as a comment but I don't yet have the ability to do so.

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

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