[英]How do I make css affect list item in .js only and not in my .html
I am currently using Visual Studio Code to develop a web application and I have 3 files index.html, styles.css and app.js Currently in my html, I am creating a navbar based on ul and li我目前正在使用 Visual Studio Code 开发一个 web 应用程序,我有 3 个文件 index.html、styles.css 和 app.js 目前在我的 html 中,我正在创建一个基于ul 和 li的导航栏
whereas in my app.js, I am also using ul li to create a list to show values pulled from my database.而在我的 app.js 中,我还使用 ul li 创建一个列表来显示从我的数据库中提取的值。
My issue is, in my css, this styles meant for my app.js only, is appearing for my index.html li items as well.我的问题是,在我的 css 中,这种样式仅适用于我的 app.js,也出现在我的 index.html li项目中。 I understand why it is happening, but I am unsure of how to fix this.我明白为什么会这样,但我不确定如何解决这个问题。
li{
padding: 20px;
background: #f6f6f6;
font-size: 20px;
color: rgb(0, 0, 0);
position: relative;
border-bottom: 1px solid #e6e6e6;
height: 80px;
}
As shown, this li css is affecting the li inside of the navbar, and the li in the list displayed on the body如图,这个li css影响了navbar里面的li,以及body上显示的list中的li
Would appreciate any help, thanks.将不胜感激任何帮助,谢谢。
Here is my html navbar codes这是我的 html 导航栏代码
<header>
<img class = "logo" src="images/logo.jpg" height = "50">
<h1 style="color:rgb(0, 0, 0);font-size:30px">Physics Tutor Portal</h1>
<nav>
<ul class = "nav_links">
<li><a href="#"><button>Unknown Questions</button></a></li>
<li><a href="#"><button>Manage Quiz</button></a></li class = "test">
<li><a href="#"><button>Quiz Statistics</button></a></li class = "test">
<li><a href="#"><button>Feedbacks</button></a></li class = "test">
</ul>
</nav>
</header>
css
is always applied to html
, you can only design or change the color of a html tag. css
始终应用于html
,您只能设计或更改 html 标签的颜色。
Your javascript
file will generate a list li
in your case but that just means that he's gonna be added to the current html
so it's potentially going to be altered by your css
.您的javascript
文件将在您的情况下生成一个列表li
,但这仅意味着他将被添加到当前的html
因此它可能会被您的css
更改。
And this is indeed the case because you stipulate a rule with li {}
which means that all the li
on your page will be affected, even the ones generated by javascript
.情况确实如此,因为您使用li {}
规定了一个规则,这意味着您页面上的所有li
都会受到影响,即使是由javascript
生成的li
。
To avoid this you will need to update your html
structure to make your css
more precise.为了避免这种情况,您需要更新您的html
结构以使您的css
更加精确。 For example: your generated list will be encapsulated in a ul
tag, the list is here to 'manage the quizz' (if I understood you correctly).例如:您生成的列表将封装在ul
标签中,该列表用于“管理测验”(如果我理解正确的话)。 So you can simply add an id
to this ul
like <ul id="quizzManager">
.所以你可以简单地向这个ul
添加一个id
,比如<ul id="quizzManager">
。 You will be able able now to write css
only for li
who are children from a ul
with the id quizzManager
just like that:您现在可以只为li
编写css
,这些li
是ul
孩子,id 为quizzManager
,就像这样:
ul#quizzManager li {
padding: 20px;
background: #f6f6f6;
font-size: 20px;
color: rgb(0, 0, 0);
position: relative;
border-bottom: 1px solid #e6e6e6;
height: 80px;
}
In this case, all li
that are not in a ul#quizzManager
will not be affected by this css
.在这种情况下,不在ul#quizzManager
中的所有li
都不会受到此css
影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.