简体   繁体   English

添加 CSS 时 JS 颜色更改停止工作

[英]JS colour change stops working when CSS added

This is a seriously ' my first crack at HTML/JS ' kind of query.这是一个严肃的“我对 HTML/JS 的第一次破解”类型的查询。

What am I trying to do?我想做什么?
I'm building a simple webpage where there is content that acts as a 'checklist'.我正在构建一个简单的网页,其中包含充当“清单”的内容。 eg - in my code below, you have finished 'example 1' so you click it, and onclick it should turn the background of that section a different colour.例如 - 在我下面的代码中,您已经完成了“示例 1”,因此您单击它,然后 onclick 它应该将该部分的背景变成不同的颜色。

What is going wrong出了什么问题
The issue I'm having is that the JS I'm using does not work when I add a CSS class/id to style it.我遇到的问题是,当我添加 CSS 类/ID 来设置它的样式时,我正在使用的 JS 不起作用。 It only works when it is a bare-bones 'li' tag.它仅在它是一个简单的“li”标签时才有效。

Ironically for the sake of posting this question;具有讽刺意味的是,为了发布这个问题; when I take the script content from the separate.js file and add it to be done in the html, neither seem to work...当我从单独的.js 文件中获取脚本内容并将其添加到 html 中时,似乎都不起作用......

JS Is very much my least experienced area, would somebody have any pointers for the below code (very quick mock up of random content, not the actual page). JS 是我经验最少的领域,有人对以下代码有任何指示吗(非常快速地模拟随机内容,而不是实际页面)。

 //COLOUR CHANGE ON-CLICK AND MOUSE HOVER $(document).ready(function() { $('li').on('click', function() { $(this).toggleClass('selected'); }); $('li').on('mouseenter', function() { $(this).toggleClass('mouseover') }); $('li').on('mouseleave', function() { $(this).toggleClass('mouseover') }); });
 .container { background-color: #0375B4; margin-bottom: 20px; }.listitem { border: 1px; background: #E2F2FF; margin: 10px; padding: 9px; font-size: 23px; list-style-type: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col"> <ul> <li class="listitem">example 1</li> <li class="listitem">example 2</li> <li class="listitem">example 3</li> </ul> </div> </div> </div> <div class="container"> <div class="row"> <div class="col"> <ul> <li>example 1</li> <li>example 2</li> <li>example 3</li> </ul> </div> </div> </div>

Add the missing classes selected , 'mouseover'.添加缺少的selected类,“鼠标悬停”。 Then you will see that your code works perfectly.然后你会看到你的代码完美运行。

working example with added class selected only仅选择添加 class 的工作示例

 $(document).ready(function() { console.log(1) $('li').on('click', function() { $(this).toggleClass('selected'); }); $('li').on('mouseenter', function() { $(this).toggleClass('mouseover') }); $('li').on('mouseleave', function() { $(this).toggleClass('mouseover') }); });
 .container { background-color: #0375B4; margin-bottom:20px; }.listitem { border: 1px; background: #E2F2FF; margin: 10px; padding: 9px; font-size: 23px; list-style-type: none; }.selected { color:red; } li { cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <div class="container"> <div class="row"> <div class="col"> <ul> <li class="listitem">example 1</li> <li class="listitem">example 2</li> <li class="listitem">example 3</li> </ul> </div> </div> </div> <div class="container"> <div class="row"> <div class="col"> <ul> <li>example 1</li> <li>example 2</li> <li>example 3</li> </ul> </div> </div> </div> <style> </style> <script> //COLOUR CHANGE ON-CLICK AND MOUSE HOVER </script>

add styling添加样式

 //COLOUR CHANGE ON-CLICK AND MOUSE HOVER $(document).ready(function() { $('li').on('click', function() { $(this).toggleClass('selected'); }); $('li').on('mouseenter', function() { $(this).toggleClass('mouseover') }); $('li').on('mouseleave', function() { $(this).toggleClass('mouseleave') }); });
 .container { background-color: #0375B4; margin-bottom: 20px; }.listitem { border: 1px; background: #E2F2FF; margin: 10px; padding: 9px; font-size: 23px; list-style-type: none; }.listitem.selected { background: red; }.listitem.mouseover { background: green; }.listitem.mouseleave { background: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col"> <ul> <li class="listitem">example 1</li> <li class="listitem">example 2</li> <li class="listitem">example 3</li> </ul> </div> </div> </div> <div class="container"> <div class="row"> <div class="col"> <ul> <li>example 1</li> <li>example 2</li> <li>example 3</li> </ul> </div> </div> </div>

You have to add styles for "selected" to be applied to the element:您必须添加 styles 才能将“选定”应用于元素:

.selected {
  background-color: red;
}

Once you click on the li element it have "selected" css class and new.一旦单击 li 元素,它就会“选择” css class 和新元素。 style will be applied.样式将被应用。 When clicked again it will be removed.再次单击时,它将被删除。 To be more precise, depending on how much you develop your code, the styles should specify the element in more details.更准确地说,根据您开发代码的程度,styles 应该更详细地指定元素。 It will be probably:大概会是:

li.selected {...}

or或者

.listitem.selected {...}

btw.顺便提一句。 Instead of toggling class when mouse is over element with JS you can have the same effect with CSS only:当鼠标悬停在带有 JS 的元素上时,而不是切换 class 您可以仅使用 CSS 获得相同的效果:

li:hover {
  background-color: yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM