weixin_33719619 2016-11-16 08:13 采纳率: 0%
浏览 63

Arduino和JS / Cpp

I have a project "smart house" and I use Arduino Mega with Ethernet Shield. My problem is connect data from arduino to GUI website (I am beginner in JS). When I press the button, the data is sent to Arduino and my button change color from red to green (light on) -> This is ok.

Then, if I open the door i would like to div color change from red to green and i dont know how..

This is Arduino code:

int count;                 // used by 'for' loops
int sw_arr[] = {30,31,32,33,34,35};  // pins interfaced to switches

for (count = 0; count < 5; count++) {
    cl.print("<switch>");
    if (digitalRead(sw_arr[count])) {;
        cl.print("OFF");
    }
    else {
        cl.print("ON");
    }
    cl.println("</switch>");
}

On my website the string change from OFF to ON, but i would like to background color change and i think that my problem is Javascript.

This is orginal code from tutorial:

var count;
var num_an = this.responseXML.getElementsByTagName('switch').length;

        for (count = 0; count < num_an; count++) {
             document.getElementsByClassName("doors")[count].innerHTML =
             this.responseXML.getElementsByTagName('switch')[count].childNodes[0].nodeValue;
        }

I change this code, but still not working:

var count;
var num_an = this.responseXML.getElementsByTagName('switch').length;

for (count = 0; count < num_an; count++) {

if(this.responseXML.getElementsByTagName('switch')[count].childNodes[0].nodeValue === "ON") {
                $('.doors').css('background', '#989898');
            }
        else {
                $('.doors').css('background', '#FA6900');
             }

        }

HTML Code:

<div class="item-door light-red doors">
        <div id = "door">  
           <i class="fa fa-key"></i>
              <p>Door</p>
        </div>
 </div>

So summary, I need change DIV background color from red to green instead string ON/OFF.

If someone have a time and could help me I would be very thankful!

  • 写回答

1条回答 默认 最新

  • 谁还没个明天 2016-11-16 08:27
    关注

    The code $('.doors').css('background', '#989898'); changes the background of all container which contains the class "doors". In you for statement, if the last iteration changes background for #FA6900, all your div will get the #FA6900 color.

    If you want to change only one container you have to write :

    $( $(".doors").get(count) ).css('background', '#989898').`

    There is two time $() because .get() return a DOM element and not a jquery element so we need to transform it again if we want to apply the .css() method.

    Last point : #989898 is grey and #FA6900 is orange

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么