日期未显示在div中

时间:2017-06-09 06:53:22

标签: javascript jquery html css

我有div的视图,我需要显示日期

我写了这个JavaScript代码



$(document).ready(function() {
  var dateObj = new Date();
  var month = dateObj.getUTCMonth() + 1; //months from 1-12
  var day = dateObj.getUTCDate();
  var year = dateObj.getUTCFullYear();

  newdate = year + "/" + month + "/" + day;
  $("#id").append(newdate);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date" style="width: 100%; height: 10%; background:#1d69b4 ">

</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

更改

$("#id").append(newdate);

$("#date").append(newdate);  

这是一个选择器,用于获取ID为日期

的元素

答案 1 :(得分:1)

你指的是错误的id。应该是public class RMIInjectionExtension implements Extension { private Set< Class< ? > > interfaces = Sets.newHashSet(); void processInjectionPoint( @Observes final ProcessInjectionPoint< ?, ? > aProcessInjectionPoint ) { // collecting all relevant interfaces if( aProcessInjectionPoint.getInjectionPoint().getQualifiers().stream() .anyMatch( RMI.class::isInstance ) ) { interfaces.add( (Class< ? >)aProcessInjectionPoint.getInjectionPoint().getType() ); } } void afterBeanDiscovery( @Observes final AfterBeanDiscovery aAfterBeanDiscovery, BeanManager aBeanManager ) { // create beans for the interfaces // using DeltaSpike BeanBuilder interfaces.stream().map( a -> new BeanBuilder< Object >( aBeanManager ).beanClass( a ) .qualifiers( (RMI)() -> RMI.class ).beanLifecycle( new RMILifecyle() ).create() ) .forEach( aAfterBeanDiscovery::addBean ); } }

#date

&#13;
&#13;
$("#date").append(newdate);
&#13;
$(document).ready(function() {
  var dateObj = new Date();
  var month = dateObj.getUTCMonth() + 1; //months from 1-12
  var day = dateObj.getUTCDate();
  var year = dateObj.getUTCFullYear();

  newdate = year + "/" + month + "/" + day;
  $("#date").append(newdate);
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您使用了错误的选择器 改变

$("#id").append(newdate); 

 $("#date").append(newdate);

答案 3 :(得分:0)

在您的HTML中,您有div id='date'并将格式化的日期字符串显示在该div中,那么您应该使用.text() or .html()代替.append()

所以你的脚本应该是

$("#date").text(newdate);

// or

$("#date").html(newdate);