如何使用 JS 将样式修改为 HTML 元素(使用 CSS 在外部设置样式)? [英] How to modify style to HTML elements (styled externally with CSS) using JS?

查看:25
本文介绍了如何使用 JS 将样式修改为 HTML 元素(使用 CSS 在外部设置样式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当我点击家庭类的 <p> 标签时,我希望它改变颜色为绿色,但它不起作用,并知道为什么.点击注册正常(因为 console.log("test") 显示得很好)但其余的更改颜色的功能将不起作用.这是我的 css、html 和 js 代码(js 代码包含在 HTML 中,所以它不是外部文件或任何东西):

so when i click on the <p> tag of the home class, i want it to change color to green but it doesn't work and idk why. the click registers fine (as the console.log("test") displays just fine) but the rest of the function to change the color won't work. here's my css, html and js code (the js code is contained in the HTML, so it's not an external file or anything):

.greyRect {

  height:150px;
  width:1350px;

  background-color: #D3D3D3;


}
h1 {
  text-align: center;
}
h2 {
    text-align: center;
}
.home {



box-sizing: border-box;
width:80px;
height:35px;

line-height: 2;
  position: relative;
left:350;
  color:white;

}
.casinocraps {
  background-color: grey;

box-sizing: border-box;
width:120px;
height:35px;
text-align: center;
line-height: 2;
  position: relative;
left:460;
bottom:50;
  color:white;
}
.tictactoe {
  background-color: grey;
  box-sizing: border-box;
  width:90px;
  height:35px;
  text-align: center;
line-height: 2;
    position: relative;
left:600;
bottom:100;
    color:white;
}
.bingo {
  background-color: grey;
  box-sizing: border-box;
  width:80px;
  height:35px;
  text-align: center;
  line-height: 2;
    position: relative;
  left:700;
  bottom:150;
    color:white;
}
.concentration {
  background-color: grey;
  box-sizing: border-box;
  width:100px;
  height:35px;
  text-align: center;
  line-height: 2;
    position: relative;
  left:800;
  bottom:200;
    color:white;
}
footer {
  text-align: center;
    line-height: 4;
      position: relative;
  top:125;
  right:15;
  height:70px;
  width:1365px;

  background-color: #D3D3D3;
}
.border {
  height: 50px;
  width: 100px;
  border: 4px solid green;
  background-color: #555;
  position: relative;
  top:20;
  left:100;
}
.rectangle {
  height: 50px;
  width: 100px;
  background-color: #555;
  position: relative;
  top:50;
  left:100;
}

<html>
  <head>

    <meta charset="utf-8">

    <title></title>
      <link rel="stylesheet" type="text/css" href="cssForAss4.css">
  </head>
  <body>

<header class="greyRect" >
<h1>Assignment 1</h1>

<h2>Home Page</h2>
<nav>
<p class="home" onclick="selectHome()">
Home
</p>
<p class="casinocraps">

<b>Casino Craps</b>
</p>
<p class="tictactoe">

<b>Tic-Tac-Toe</b>
</p>
<p class="bingo">

<b>Bingo</b>
</p>
<p class="concentration">

<b>Concentration</b>
</p>
</nav>
<div class="border">
</div>
<footer >Footer</footer>

</header>

<script>
function selectHome() {
  console.log("test");

document.getElementsByClassName("home").style += "background-color:green;";


}
</script>
  </body>
</html>

<页脚>页脚</页脚></标题><脚本>功能选择家(){控制台日志(测试");document.getElementsByClassName("home").style += "background-color:green;";}</html>

推荐答案

其他人已经建议 .getElementsByClassName("home")[0],这是一个糟糕的主意.

First, .getElementsByClassName() returns a node list of all the matching elements. If you are only interested in the first one, it makes no sense to find that one and then keep scanning for more matches and then discard all but the first one found, which is what this code does.

首先,.getElementsByClassName() 返回所有匹配元素的节点列表.如果您只对第一个感兴趣,那么找到那个然后继续扫描更多匹配项然后丢弃除找到的第一个之外的所有匹配项是没有意义的,这就是此代码的作用.

Second, .getElementsByClassName() returns a "live" node list. This means that every time you interact with the list, the entire DOM is searched again for matches, ensuring that you have the most up to date set in your list. This can be useful in applications where nodes are being added and removed dynamically, but those use cases aren't as common.

其次,.getElementsByClassName() 返回一个live"节点列表.这意味着每次您与列表交互时,都会再次搜索整个 DOM 以查找匹配项,确保您在列表中设置了最新的内容.这在动态添加和删除节点的应用程序中很有用,但这些用例并不常见.

FYI: .getElementsByTagName(), .getElementsByName(), and node.childNodes also return live node lists.

仅供参考:.getElementsByTagName().getElementsByName()node.childNodes 也返回活动节点列表.

All of these previously mentioned methods date back to the earliest days of the DOM API, when it was still the "wild west" days of web development. They are all over two decades old and have much better alternatives today (i.e. .querySelector(), .querySelectorAll(), .closest()).

所有这些前面提到的方法都可以追溯到 DOM API 的早期,当时它仍然是狂野的西部".网络开发的日子.它们都有 20 多年的历史,今天有更好的替代方案(即 .querySelector().querySelectorAll().closest()).

When it's not necessary to keep an up to date list, .querySelectorAll() is the way to go. And frankly, even if you do need an updated node list, you're still better off with .querySelectorAll() and just run it again manually at the point where you need an updated list.

当不需要保持最新列表时,.querySelectorAll() 是要走的路.坦率地说,即使您确实需要更新的节点列表,您仍然最好使用 .querySelectorAll() 并在需要更新列表的地方再次手动运行它.

Here's a good page that discusses this and here's what it has to say:

这是一个 好的页面,讨论了这个问题,下面是它要说的内容:><块引用>

如何思考活体?

How to Think About Live Object?

活动对象不直观.你可以认为它是延迟的评估或惰性评估.活动对象的方法或属性是在访问他们的结果时重新计算.

Live object is not intuitive. You can think of it as delayed evaluation or lazy evaluation. Method or property of live object is re-computed when their result is accessed.


但是,在这种情况下,我们甚至不需要节点列表,我们只需要一个节点.正确的解决方案是:


But, in this case, we don't even want a node list, we just want a single node. The correct solution would be:

document.querySelector(".home");

.querySelector() 扫描文档中与提供的选择器匹配的第一个元素,如果找到,则返回对该单个节点的引用.否则,它返回 undefined.

这篇关于如何使用 JS 将样式修改为 HTML 元素(使用 CSS 在外部设置样式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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