首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么它说我不能设置属性fontSize,因为有些东西是未定义的?

为什么它说我不能设置属性fontSize,因为有些东西是未定义的?
EN

Stack Overflow用户
提问于 2019-07-18 19:49:10
回答 2查看 397关注 0票数 0

我最近做了一个为期5天的训练营,在开始完整的课程之前,我正试图自学更多。我尝试使用和在bootcamp中一样的"mouseover“函数来增加fontSize,但是它显示不能设置fontSize属性。我相信我已经设置了我想要更改的Id,并指定了我想要更改它的程度。

老实说,我对此非常陌生,只是想克服我的第一个Javascript障碍。

代码语言:javascript
复制
function thingHover() {
  document.getElementById('Thing').firstChild.style.fontSize = '300%';
}

function thingNormal() {
  document.getElementById('Thing').firstChild.style.fontSize = '100%';
}
代码语言:javascript
复制
<p id='Thing' onmouseover="thingHover()" onmouseout="thingNormal"> Dunno what I'm gonna do to this text yet, maybe add some JS? </p>

在新兵训练营中,我们将其应用于图标而不是文本。图标似乎悬停在其原始位置上方,略大一些。我不想仅仅因为我的文本代码不起作用就复制粘贴图标代码。

EN

回答 2

Stack Overflow用户

发布于 2019-07-18 19:53:29

下面代码中有拼写错误,请在调用时添加函数括号

代码语言:javascript
复制
function thingHover() {
           document.getElementById('Thing').style.fontSize='300%'; //remove firstChild
         }
         function thingNormal() {
           document.getElementById('Thing').style.fontSize='100%'; //remove firstChild
         }
           </script>
代码语言:javascript
复制
<p id='Thing' onmouseover="thingHover()" onmouseout="thingNormal()"> Dunno what I'm gonna do to this text yet, maybe add some JS? </p> <!--add  space between two attributes and add () on onmouseout-->

票数 0
EN

Stack Overflow用户

发布于 2019-07-18 20:04:44

您没有任何子元素,因此没有第一个子元素:

我做了一些小的编辑,下面是一个有效的代码片段。

代码语言:javascript
复制
  function thingHover() {
   var thing = document.getElementById('Thing');
   thing.firstElementChild.style.fontSize = '300%';
  }
  function thingNormal() {
  var thing = document.getElementById('Thing');
    thing.firstElementChild.style.fontSize = '100%';
  }
代码语言:javascript
复制
<script>

</script>
<div id='Thing'onmouseover="thingHover()" onmouseout="thingNormal()">
<p style='fontSize:100%;'>Dunno what I'm gonna do to this text yet, maybe add some JS?</p>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57093778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档