改变HTML中LED的颜色。

huangapple go评论82阅读模式
英文:

Change the color of a led on html

问题

这是您要翻译的部分:

style.css

.led-green {
  position: relative;
  right: -50%;
  top: 10%;
  margin: 0 auto;
  width: 24px;
  height: 24px;
  background-color: #ABFF00;
}

.led-red {
  position: relative;
  right: -50%;
  top: 10%;
  margin: 0 auto;
  width: 24px;
  height: 24px;
  background-color: rgb(255, 0, 0);
}

app.js

/** 检查互联网连接是否可用 */
var internetConnection = "";
require('dns').resolve('www.google.com', function(err) {
  if (err) {
    console.log("没有互联网连接");
    internetConnection = "led-red";
  } else {
    console.log("已连接到互联网");
    internetConnection = "led-green";
  }

  console.log(internetConnection);
});

index.ejs

<div class="<%=internetConnection%>"></div>

Update

res.render('index', {
  // ...
  internetConnection: internetConnection,
  // ...
});

UPDATE

问题不在于渲染。问题是当我将div定义如下时,HTML页面不起作用:

<div class="<=%internetconnection%>"></div>

有没有任何解决方法?再次感谢任何提示!

英文:

I'm to change the color of led based on whether of internet connection is there or not.
here's what I have done so far:

style.css

.led-green {
  position: relative;
  right: -50% ;
  top: 10%;
  margin: 0 auto;
  width: 24px;
  height: 24px;
  background-color: #ABFF00;

}
.led-red {
  position: relative;
  right: -50% ;
  top: 10%;
  margin: 0 auto;
  width: 24px;
  height: 24px;
  background-color: rgb(255, 0, 0);

}

app.js

 /** Check if internet connection is avaiable */
  var  internetConnection =&quot;&quot;
  require(&#39;dns&#39;).resolve(&#39;www.google.com&#39;, function(err) {
    if (err) {
       console.log(&quot;No internet connection&quot;);
       internetConnection =&quot;led-red&quot;;
    } else {
       console.log(&quot;Connected to internet &quot;);
       internetConnection =&quot;led-green&quot;;

    }

    console.log(internetConnection)
  });

I guess this is the part where I got the problem

index.ejs

  &lt;div class=&quot;&lt;%=internetConnection%&gt;&quot;&gt; &lt;/div&gt;

my hope was that by starting the app the division would be set like :

&lt;div class =&quot;led-green&quot;&gt; &lt;/div&gt; 

or

&lt;div class =&quot;led-red&quot;&gt; &lt;/div&gt; 

any idea how may I solve this?

thanks in advance !

Update

I'm the variable in the render function as follows :

.......................
 res.render(&#39;index&#39;, {
.....
internetConnection:internetConnection,
...............
}

UPDATE

the problem isn't the rendering. the issue is that the html page doens't work when I define div as followed :

<div class="<=%internetconnection>"> <div>

any idea how to solve this ?
thanks again for any hint !

答案1

得分: 2

可能你的响应在你的应用程序决定互联网连接是否可用之前就被呈现了吗?看起来像是你面临的问题,因为Node.js是异步的。

尝试将你用于向客户端发送响应的代码放在dns.resolve()回调中,像这样:

var internetConnection = "";
require('dns').resolve('www.google.com', function(err) {
  if (err) {
     console.log("没有互联网连接");
     internetConnection = "led-red";
     ejs.render(errorHTMLString, data, options);
  } else {
     console.log("已连接到互联网");
     internetConnection = "led-green";
     ejs.render(noErrorHTMLString, data, options);
  }
});

希望这有所帮助。

英文:

Is it possible that your response is rendered before your app decides whether internet connection is available or not? Seems to me like a problem you are facing because nodejs is asynchronous.

try to put the code you are using to send response to the client in dns.resolve() callback, like:

 var  internetConnection =&quot;&quot;
  require(&#39;dns&#39;).resolve(&#39;www.google.com&#39;, function(err) {
    if (err) {
       console.log(&quot;No internet connection&quot;);
       internetConnection =&quot;led-red&quot;;
       ejs.render(errorHTMLString, data, options);
    } else {
       console.log(&quot;Connected to internet &quot;);
       internetConnection =&quot;led-green&quot;;
       ejs.render(noErrorHTMLString, data, options);
    }

    
  });

Hope this helps.

答案2

得分: 1

请确保在resolve()的回调函数内调用render()函数:

  var internetConnection = "";
  require('dns').resolve('www.google.com', function(err) {
    if (err) {
       console.log("没有互联网连接");
       internetConnection = "led-red";
    } else {
       console.log("已连接到互联网");
       internetConnection = "led-green";
    }

    console.log(internetConnection);
    res.render('index', {
      internetConnection: internetConnection,
    });
  });
英文:

Please make sure that you call the render() function within the callback of resolve():

  var  internetConnection =&quot;&quot;
  require(&#39;dns&#39;).resolve(&#39;www.google.com&#39;, function(err) {
    if (err) {
       console.log(&quot;No internet connection&quot;);
       internetConnection =&quot;led-red&quot;;
    } else {
       console.log(&quot;Connected to internet &quot;);
       internetConnection =&quot;led-green&quot;;

    }

    console.log(internetConnection);
    res.render(&#39;index&#39;, {
      internetConnection:internetConnection,
    }
  });
 

huangapple
  • 本文由 发表于 2020年1月6日 16:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定