Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能改变<;部门>;使用javascript的背景色?_Javascript_Html - Fatal编程技术网

我怎样才能改变<;部门>;使用javascript的背景色?

我怎样才能改变<;部门>;使用javascript的背景色?,javascript,html,Javascript,Html,每次过期日期与今天的日期相同时,我都会尝试更改颜色。我在想,是否可以使用if-else语句 这是我现在的代码: <div class="col-md-6"> <div class="alert alert-info" role="alert"><span class="glyphicon glyphicon-book" aria-hidden="true"></span>&nbsp;&nbsp;Passpor

每次过期日期与今天的日期相同时,我都会尝试更改
颜色。我在想,是否可以使用
if-else语句

这是我现在的代码:

    <div class="col-md-6">
        <div class="alert alert-info" role="alert"><span class="glyphicon glyphicon-book" aria-hidden="true"></span>&nbsp;&nbsp;Passport <?php echo "<strong><a href=\"view_latest_passport.php?id=$id\"> $passport</a> /&nbsp; Passport Expiration Date:</strong> $passport_expiration"; ?></div>
    </div>

护照

如果您需要,这里有一个使用AJAX的原始JavaScript示例,我强烈推荐:

//<![CDATA[
var pre = onload, E, cN, doc, bod, phpize, post, changeBackgroundColorOnExp;
onload = function(){
if(pre)pre();
doc = document; bod = doc.body;
E = function(id){
  return doc.getElementById(id);
}
cN = function(elem, className){
  if(doc.getElementsByClassName){
     return elem.getElementsByClassName(className);
  }
  else{
    var all = doc.getElementsByTagName('*'), r = [];
    for(var i=0,l=all.length; i<l; i++){
      var ai = all[i];
      if(ai.className === className){
        r.push(ai);
      }
    }
    return r;
  }
}
phpize = function(obj, ignore){
  var r = [];
  for(var i in obj){
    if(obj.hasOwnProperty(i)){
      var p = ignore ? ignore+'['+i+']' : i;
      var v = obj[i];
      var s = typeof v === 'object' ? phpize(v, p) : encodeURIComponent(p)+'='+encodeURIComponent(v);
      r.push(s);
    }
  }
  return r.join('&');
}
post = function(where, send, success, context){
  var x = new XMLHttpRequest || new ActiveXObject('Microsoft.XMLHTTP');
  var c = context || this;
  x.open('POST', where); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  x.onreadystatechange = function(){
    if(x.readyState === 4 && x.status === 200){
      if(success)success.call(c, eval('('+x.responseText+')'));
    }
  }
  x.send(phpize(send));
}
changeBackgroundColorOnExp = function(elem, color, exp){
  if(exp){
    elem.style.backgroundColor = color;
  }
}
post('pageYouGetDataFrom.php', {dateTest:1}, function(res){
  changeBackgroundColorOnExp(cN('col-md-6')[0], '#700', res.exp);
}
}
//]]>
//
现在进入pageYouGetDataFrom.php

<?php
date_default_timezone_set('America/Vancouver'); // change to your timezone
if(isset($_POST['dateTest']) && $_POST['dateTest'] === '1'){
  $req = array(); $td = new DateTime; $xd = new DateTime($resultFromQuery);
  if($td->getTimestamp() > $xd->getTimeStamp()){
    $req['exp'] = true;
  }
  else{
    $req['exp'] = false;
  }
  echo json_encode($req);
}
?>


当然,由于这看起来只是在页面加载时进行的,所以您可以更改代码的底部部分,并在初始页面上使用它,但我认为您将来会欣赏这个AJAX示例。无论如何,您都不想使用客户端时间,因为它可以由浏览器用户更改。

在JavaScript中这当然是可能的,但我认为更好的解决方案是定义一个CSS类,如果日期是今天,则使用PHP将该类写入div。假设默认值为(未过期)颜色设置在CSS中。到目前为止,您尝试了什么?我尝试了下面的代码,但代码不起作用