为什么getElementsByTagName不起作用? [英] Why is getElementsByTagName not working?

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

问题描述

此代码到底有什么问题?

What exactly is wrong with this code?

<p id="demo">Hello World!</p> 

<script>
document.getElementsByTagName('P').style.backgroundColor = 'yellow';
</script>

背景颜色没有变为黄色.

The background color is not getting changed to yellow.

推荐答案

getElementsByTagName()返回对象数组.您需要指定索引才能应用样式属性.

getElementsByTagName() returns an array of objects. You need to specify the index inorder to apply the style property.

使用

document.getElementsByTagName('p')[0].style.backgroundColor = 'yellow';

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

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