如何用javascript改变底部的风格?

时间:2017-05-27 01:05:30

标签: javascript html css

我不知道为什么底部的风格没有变化。 我试图保存这个值并用它来改变底部的颜色,但它没有改变。控制台中没有出现语法错误,所以我想我在这方面没问题。

private static int[] wordLengths(String[] words)
{
    String[] words = in.nextLine();
    String[] array = 0;

    if(words > 0)
    {
        int[] wordLengths = words.length();
    }
    return
}

4 个答案:

答案 0 :(得分:1)

这里有多个问题。

  1. "Five"无效。也许你的意思是键入type="bottom"
    1. 您无法直接将元素type="button"设置为颜色。

      你必须更具体。你的意思是背景颜色吗?文字颜色?边框颜色?

      如果您希望更改背景颜色,请考虑以下事项:

      style
      1. 您的document.getElementById("Cblue").style.backgroundColor = "blue"; 逻辑没有意义。

        它说"如果蓝色按钮的值为"点击此处获取蓝色"当页面加载时,该函数应将其更改为蓝色。"这将始终为真,因此无论您点击什么或做什么,这都是唯一会发生的事件。

        不要使用条件逻辑,只需将您想要的颜色作为参数/参数传递给函数。此外,如果您不想更改单击的元素,而是更改不同的元素,您也可以将其作为参数传递。

        基本示例:

      2. 
        
        if
        
        function changeColor(elem, color) {
            elem.style.backgroundColor = color;
        }
        
        
        

答案 1 :(得分:0)

style是“持有css”,如果你想修改一个属性(让我们说color)然后做{your stuff here to get the DOM element ...}.style.color = "blue"

另外,你永远不会到达else if if,因为它等同于: if( !(colorb === "click here for blue") && (colorg === "click here for green") )。由于colorb 始终等于“点击此处获取蓝色”,因此您永远不会进入该区块。 要解决此问题,只需结束第一个if语句,然后让else if成为简单的if语句:

if(conditionA){}else if(conditionB){}将成为if(conditionA){} if(conditionB){}

答案 2 :(得分:0)

问题是style这里不是目标css属性:

 document.getElementById("Cgreen").style = "green";

使用普通JS:供后台使用

 document.getElementById("Cgreen").style.backgroundColor = "green";

使用jQuery:供后台使用

$("#Cgreen").css("background","green");
  

我不知道为什么底部的风格没有变化。

点击后,你的代码没有计算出来,点击了哪个html元素。

function changeColor(button) {
  button.style.backgroundColor = button.id.substr(1);
  /* here substr(1) will produce
  Cgreen = green, 
  Cblue = blue 
  */
}
<input type="button" class="btn" id="Cgreen" value="click here for green" onclick="changeColor(this)" />
<input type="button" class="btn" id="Cblue" value="click here for blue" onclick="changeColor(this)" />

试试这段代码:

答案 3 :(得分:0)

&#13;
&#13;
$('.btn').click(function(e){
  changeColor($(this));
});

function changeColor(element){
  var colorbutton = element.val();
  if (colorbutton === "click here for blue") {
    $('body').css('backgroundColor','blue');
  }else if (colorbutton === "click here for green"){
    $('body').css('backgroundColor','green');
  }

}
&#13;
<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
 <meta charset="utf-8">
 <link rel="stylesheet"  href="main.css">
    <title>testing</title>

</head>
<body id="body">
<form>
    <input type="button"  class="btn" id="Cgreen" value="click here for green">
    <input type="button"  class="btn" id="Cblue" value="click here for blue">
</form>
</body>
</html>
javascript 
&#13;
&#13;
&#13;

我不知道这个结果是否适合您的目的?希望它能解决你的问题。我看到你使用jquery库,所以我用jquery重写。