- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设计了一个表格日历,在表格主体下有一堆 TD/TR 元素。
我希望在表格的每一天进行交互,例如当我单击一个 td 元素(即一天)时,它将用边框突出显示,当我移动光标并单击其他一天时,这一天将突出显示,而前一天将被取消-突出显示。我的代码是这样的,但问题是 .off 点击功能。它不是取消突出显示,因此所有表格单元格都会突出显示并保留。我如何使用 jQuery 来解决这个问题?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("td.PTDC").on("click",function(){
$(this).css("background-color", "#0093e0");
$(this).css("padding", "5px");
console.log("onclick");
});
$("tbody").off("click",function(){
$(this).css("background-color", "#ffffff");
$(this).css("padding", "0px");
console.log("offclick");
});
});
</script>
<td class="PTDC PTLC OOCT" id="db_saw_1883_7_1_6" style="border-style:none;border-right:solid 1px #DADADB;border-bottom:solid 1px #DADADB;">
<td class="PTDC OOCT" id="db_saw_1883_7_1_5" style="border-style: none solid solid none; border-right-width: 1px; border-right-color: rgb(218, 218, 219); border-bottom-width: 1px; border-bottom-color: rgb(218, 218, 219); background-color: rgb(0, 147, 224); padding: 5px;">
最佳答案
更新 2
OP 正在使用类样式无法覆盖的原始应用程序。我从有关工具的各种线索中推断出(OP 含糊不清):
= 生成...HTML 表格
- 它使用内联样式
- 如果是这样,那么这将解释为什么使用类进行样式设计非常困难。
- 内联样式(例如 <div style='color:blue'>
)不能被样式表中的规则集覆盖,甚至不能被 <style>
覆盖 block 与 !important 是异常(exception)。 演示3 将演示 2 种处理内联样式属性的方法。
$('td').on('click', function(e) {
var tgt = e.target;
e.stopImmediatePropagation();
$('td').each(function(idx, cell) {
cell.style.backgroundColor = '#fff';
cell.style.borderColor = '#000';
if ($(cell).hasClass('today')) {
cell.style.backgroundColor = 'rgba(0, 0, 255, 1)';
cell.style.borderColor = '#aae1ff';
}
});
tgt.style.backgroundColor = '#0093e0';
tgt.style.borderColor = '#09e';
});
e.target
是 <td>
用户点击了。 e.stopImmediatePropagation();
防止事件冒泡并被任何其他监听器听到。 $('td').each(function(idx, cell) {...
每<td>
将在其上运行一个函数。 <td>
)将其内联样式属性设置为: cell.style.backgroundColor = '#fff';
cell.style.borderColor = '#000';
.today
类,然后:if ($(cell).hasClass('today')) {
cell.style.backgroundColor = 'rgba(0, 0, 255, 1)';
cell.style.borderColor = '#aae1ff';
}
for
循环完成,更改e.target
风格: tgt.style.backgroundColor = '#0093e0';
tgt.style.borderColor = '#09e';
.lit
类(class)。使用
.addClass()
可以轻松修改,
.removeClass()
和
.not()
.见演示 2。
/* Delegate the click event on all
|| td (cell).
|| Remove the .lit class on every <td>
|| and add .lit class for the clicked <td>
*/
$('td').on('click', function() {
var that = $(this);
$('td').not(that).removeClass('lit');
that.addClass('lit');
});
"...but the problem is the
.off
click function. It is not unhighlighting so all table cells become highlighted and persists. How could I fix this using jQuery?"
.on()
方法是一个添加事件监听器的函数
在 任何给定的个人或一组元素(例如
$('td')
)。
.off()
方法是一个删除事件监听器的函数
关闭 任何给定的个体或元素组。
.off()
不撤消任何
.on()
已完成,
.off()
删除
.on()
.所以每
<td>
单击然后丢失注册到它的事件监听器。
.css()
设置一组元素样式的方法.lit
是另一个状态,默认状态是 <td>
无类.lit
.toggleClass()
是用于执行此操作的方法。 // Make a Date Object
var d = new Date();
// Get today's day as a number
var today = d.getDate();
/* Find the cell at the index number
|| (which is eq -1) and add thr .today class
*/
$('td').eq(today - 1).addClass('today');
/* On each cell, add the day number, unless
|| the cell has class .empty
*/ // Note: the syntax of string on line 19
// is ES6 Template Literal see post for ref.
$('td').each(function(index, day) {
if ($(this).hasClass('empty')) {
return
}
$(this).append(`<b>${index+1}</b>`);
});
/* Delegate the click event on all
|| td (cell).
|| callback on each td is to
|| toggle the .lit class
*/
$('td').on('click', function() {
$(this).toggleClass('lit');
});
.month {
table-layout: fixed;
width: 90%;
min-height: 250px;
border-spacing: 1px;
border: 3px outset grey
}
caption {
font-variant: small-caps
}
.month td {
border: 2px inset black;
background: #fff;
cursor: pointer;
}
td.lit {
background-color: #0093e0;
border-color: #09e;
}
td.today {
background: rgba(0, 0, 255, 1);
border-color: #aae1ff;
}
td.today.lit {
background: tomato;
border-color: red
}
td b {
font-size: .3em;
color: #123;
vertical-align: top;
display: inline-block;
margin: -7px 0 0 -5px;
}
td.today b {
color: #fff
}
.empty {
/* Prevents any mouse events
|| i.e unclickable
*/
pointer-events: none;
cursor: default;
}
<table class='month'>
<caption>October</caption>
<thead>
<tr>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='empty' colspan='4'> </td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
// Make a Date Object
var d = new Date();
// Get today's day as a number
var today = d.getDate();
/* Find the cell at the index number
|| (which is eq -1) and add thr .today class
*/
$('td').eq(today - 1).addClass('today');
/* On each cell, add the day number, unless
|| the cell has class .empty
*/// Note: the syntax of string on line 19
// is ES6 Template Literal see post for ref.
$('td').each(function(index, day) {
if ($(this).hasClass('empty')) {
return
}
$(this).append(`<b>${index+1}</b>`);
});
/* Delegate the click event on all
|| td (cell).
|| Remove the .lit class on every <td>
|| and add .lit class for the clicked <td>
*/
$('td').on('click', function() {
var that = $(this);
$('td').not(that).removeClass('lit');
that.addClass('lit');
});
.month {
table-layout: fixed;
width: 90%;
min-height: 250px;
border-spacing: 1px;
border: 3px outset grey
}
caption {
font-variant: small-caps
}
.month td {
border: 2px inset black;
background: #fff;
cursor: pointer;
}
td.lit {
background-color: #0093e0;
border-color: #09e;
}
td.today {
background: rgba(0, 0, 255, 1);
border-color: #aae1ff;
}
td.today.lit {
background: tomato;
border-color: red
}
td b {
font-size: .3em;
color: #123;
vertical-align: top;
display: inline-block;
margin: -7px 0 0 -5px;
}
td.today b {
color: #fff
}
.empty {
/* Prevents any mouse events
|| i.e unclickable
*/
pointer-events: none;
cursor: default;
}
<table class='month'>
<caption>October</caption>
<thead>
<tr>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='empty' colspan='4'> </td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
// Make a Date Object
var d = new Date();
// Get today's day as a number
var today = d.getDate();
/* Find the cell at the index number
|| (which is eq -1) and add thr .today class
*/
$('td').eq(today - 1).addClass('today');
/* On each cell, add the day number, unless
|| the cell has class .empty
*/ // Note: the syntax of string on line 19
// is ES6 Template Literal see post for ref.
$('td').each(function(index, day) {
if ($(this).hasClass('empty')) {
return
}
$(this).append(`<b>${index+1}</b>`);
});
/* Delegate the click event on all
|| td (cell).
|| See post update for details
||
*/
$('td').on('click', function(e) {
var tgt = e.target;
e.stopImmediatePropagation();
$('td').each(function(idx, cell) {
cell.style.backgroundColor = '#fff';
cell.style.borderColor = '#000';
if ($(cell).hasClass('today')) {
cell.style.backgroundColor = 'rgba(0, 0, 255, 1)';
cell.style.borderColor = '#aae1ff';
}
});
tgt.style.backgroundColor = '#0093e0';
tgt.style.borderColor = '#09e';
});
.month {
table-layout: fixed;
width: 90%;
min-height: 250px;
border-spacing: 1px;
border: 3px outset grey
}
caption {
font-variant: small-caps
}
.month td {
border: 2px inset black;
background: #fff;
cursor: pointer;
}
td.lit {
background-color: #0093e0;
border-color: #09e;
}
td.today {
background: rgba(0, 0, 255, 1);
border-color: #aae1ff;
}
td.today.lit {
background: tomato;
border-color: red
}
td b {
font-size: .3em;
color: #123;
vertical-align: top;
display: inline-block;
margin: -7px 0 0 -5px;
background: rgba(0, 0, 0, 0);
pointer-events: none;
}
td.today b {
color: #fff
}
.empty {
/* Prevents any mouse events
|| i.e unclickable
*/
pointer-events: none;
cursor: default;
}
<table class='month'>
<caption>October</caption>
<thead>
<tr>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class='empty' colspan='4'> </td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
.not()
关于jquery - 选择 jQuery 的表格单元格颜色突出显示和取消突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509722/
是否有某种方法可以使用 JPA 或 Hibernate Crtiteria API 来表示这种 SQL?或者我应该将其作为 native 执行吗? SELECT A.X FROM (SELECT X,
在查询中, select id,name,feature,marks from (....) 我想删除其 id 在另一个 select 语句中存在的那些。 从 (...) 中选择 id 我是 sql
我想响应用户在 select 元素中选择一个项目。然而这个 jQuery: $('#platypusDropDown').select(function () { alert('You sel
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个回答) 关闭8年前。 我正在学习 SQL
This question already has answers here: “Notice: Undefined variable”, “Notice: Undefined index”, and
我在 php 脚本中调用 SQL。有时“DE”中没有值,如果是这种情况我想从“EN”中获取值 应该是这样的,但不是这样的 IF (EXISTS (SELECT epf_application_deta
这可能是一个奇怪的问题,但不知道如何研究它。执行以下查询时: SELECT Foo.col1, Foo.col2, Foo.col3 FROM Foo INNER JOIN Bar ON
如何在使用 Camera.DestinationType.FILE_URI. 时在 phonegap camera API 中同时选择或拾取多个图像我能够一次只选择一张图像。我可以使用 this 在
这是一个纯粹的学术问题。这两个陈述实际上是否相同? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 相对 IF EXIS
我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。 我的伪代码是这样的 Document divTag = Jsoup.connect(link
我正在处理一个具有多个选择框的表单。当用户从 selectbox1 中选择一个选项时,我需要 selectbox2 active 的另一个值。同样,当他选择 selectbox2 的另一个值时,我需要
Acme Inc. Christa Woods Charlotte Freeman Jeffrey Walton Ella Hubbard Se
我有一个login.html其中form定义如下: First Initial Plus Last Name : 我的do_authorize如下: "; pri
$.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data
我有一个下拉列表“磅”、“克”、“千克”和“盎司”。我想要这样一种情况,当我选择 gram 来执行一个函数时,当我在输入字段中输入一个值时,当我选择 pounds 时,我想要另一个函数来执行时我在输入
我有一个 GLSL 着色器,它从输入纹理的 channel 之一(例如 R)读取,然后写入输出纹理中的同一 channel 。该 channel 必须由用户选择。 我现在能想到的就是使用一个 int
我想根据下拉列表中的选定值生成输入文本框。 Options 2 3 4 5 就在这个选择框之后,一些输入字段应该按照选定的数字出现。 最佳答案 我建议您使用响应式(Reac
我是 SQL 新手,我想问一下如何根据首选项和分组选择条目。 +----------+----------+------+ | ENTRY_ID | ROUTE_ID | TYPE | +------
我有以下表结构: CREATE TABLE [dbo].[UTS_USERCLIENT_MAPPING_USER_LIST] ( [MAPPING_ID] [int] IDENTITY(1,1
我在移除不必要的床单时遇到了问题。我查看了不同的论坛并将不同的解决方案混合在一起。 此宏删除工作表(第一张工作表除外)。 Sub wrong() Dim sht As Object Applicati
我是一名优秀的程序员,十分优秀!