为什么getElementsByClassName不起作用? [英] Why isn't getElementsByClassName working?

查看:68
本文介绍了为什么getElementsByClassName不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么getElementsByClassName在这里不起作用.

I can't understand why getElementsByClassName isn't working here.

  n = new Date();
  y = n.getFullYear();
  document.getElementByClassName("date").innerHTML = y;

<span class="date"></span> CompanyName

推荐答案

  1. 函数名称为 getElementsByClassName ,而不是 getElementByClassName
  2. getElementsByClassName 返回元素的数组形式对象,而不是元素之一.
  1. The function name is getElementsByClassName, not getElementByClassName
  2. getElementsByClassName returns an array-like object of elements, not the one of elements.

代码应如下所示:

n = new Date();
y = n.getFullYear();
document.getElementsByClassName("date")[0].innerHTML = y;

<span class="date"></span> CompanyName

这篇关于为什么getElementsByClassName不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆