gpt4 book ai didi

javascript - ¿如何防止在单击分页按钮或标题列时重置 JQuery 数据表样式?

转载 作者:行者123 更新时间:2023-12-02 21:50:56 25 4
gpt4 key购买 nike

注意:没有必要查看所有 HTML 代码,代码很大,因为我想显示很多页面来描述我的问题。

我只使用数据表的过滤和分页功能,我不需要样式,我想为我自己的样式添加样式。但我有一个问题,当我点击一些分页链接或标题进行排序时,我在分页链接上放置的样式消失了,我不知道为什么。

我尝试为我的样式添加 !important 关键字来赢得特异性,但这不起作用。

是否有一些方法可以让我的样式在点击时不被重置?

这是我的代码、样式 html 和插件的初始化:

        $(document).ready(function () {
$.fn.DataTable.ext.pager.numbers_length = 5
$('#news').DataTable({
rowReorder: true,
dom: '<<"filter-controls-wrapper"f><"table-box" t><"pagination-wrapper"p>>'

});

let paginationElements = document.querySelectorAll("#news_paginate > span .paginate_button");

paginationElements.forEach(function (value, key, nodeList) {
value.setAttribute("class", "label-page")
value.setAttribute("id","toggle-" + key)

})
// window.filterControls.appendChild(window.filterOptions);
// window.paginationControl.appendChild(window.paginationOptions);
});
        body {
display: flex;
margin: 0;
padding: 0;
padding-left: 1rem;
padding-right: 1rem;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #ffffff;
font-family: san-serif;
}

.table-box {
width: 890px;
height: 400px;
overflow-y: scroll;
box-shadow: 0 10px 100px rgba(0, 0, 0, 0.5);
margin-left: auto;
margin-right: auto;
}

table {
width: 100%;
}

table,
th,
td {
border: 1px solid #005277;
border-collapse: collapse;
}

tr th:nth-child(1) {
backround-color: pink;
}

th {
background-color: #1e305b;
color: white;
}

th,
td {
padding: 10px;
}

thead {
position: sticky;
top: 0;
}

.news-section {
display: grid;
grid-template-rows: ""
}

.filter-controls-wrapper {
margin-bottom: 16px;
display: flex;
justify-content: flex-end;
}


input[type="search"] {
border: 1px solid transparent;
border-radius: 2rem;
padding-bottom: 5px;
padding-left: 16px;
padding-right: 16px;
padding-top: 16px;
padding-bottom: 16px;
transition: 0.2s border-color ease-in-out;
}

* {
font-family: 'Source Sans Pro', sans-serif;
}

label {
font-size: 24px;
}

input[type="search"]:focus {
border-color: navy;

}


/*PAgination here*/
.label-page {
width: 80px ;
height: 25px ;
margin: 0 10px ;
border-radius: 999px ;
background: crimson ;
cursor: pointer;
box-shadow:
0 5px 5px -5px rgba(crimson, 0.15),
0 10px 10px -5px rgba(crimson, 0.15),
0 15px 15px -5px rgba(crimson, 0.15),
0 20px 20px -5px rgba(crimson, 0.15) ;
transition: 0.25s ease;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
padding: 10px;
}

[id*="toggle-"]:hover{
width: 120px;
}

.dataTables_paginate {
margin-top: 16px;
display: flex;
justify-content: center;
align-items: center;
}
.dataTables_paginate span{
display: flex;
justify-content: center;
align-items: center;
}
    <table id="news">
<thead>
<tr>
<th>Titulo</th>
<th>Opciones</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>
<a href="#"><span class="fa fa-edit"></span></a>
<a href="#"><span class="fa fa-eye-slash"></span></a>
<a href="#"><span class="fa fa-trash"></span></a>
</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>
<a href="#"><span class="fa fa-edit"></span></a>
<a href="#"><span class="fa fa-eye-slash"></span></a>
<a href="#"><span class="fa fa-trash"></span></a>
</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr> <tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>

</tbody>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/rowreorder/1.2.6/js/dataTables.rowReorder.min.js"></script>

最佳答案

问题是您更改了分页按钮的类并将您的样式应用到这个“自定义”类。这只有效一次,因为每次您导航时数据表都会重新绘制按钮并且您的自定义类会消失。

    let paginationElements = document.querySelectorAll("#news_paginate > span .paginate_button");

paginationElements.forEach(function (value, key, nodeList) {
value.setAttribute("class", "label-page")
value.setAttribute("id","toggle-" + key)

})

删除整个代码块并将您的样式应用到默认的 .pagination_button 可以解决您的问题。

$(document).ready(function () {
$.fn.DataTable.ext.pager.numbers_length = 5
$('#news').DataTable({
rowReorder: true,
dom: '<<"filter-controls-wrapper"f><"table-box" t><"pagination-wrapper"p>>'

});

// window.filterControls.appendChild(window.filterOptions);
// window.paginationControl.appendChild(window.paginationOptions);
});
body {
display: flex;
margin: 0;
padding: 0;
padding-left: 1rem;
padding-right: 1rem;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #ffffff;
font-family: san-serif;
}

.table-box {
width: 890px;
height: 400px;
overflow-y: scroll;
box-shadow: 0 10px 100px rgba(0, 0, 0, 0.5);
margin-left: auto;
margin-right: auto;
}

table {
width: 100%;
}

table,
th,
td {
border: 1px solid #005277;
border-collapse: collapse;
}

tr th:nth-child(1) {
backround-color: pink;
}

th {
background-color: #1e305b;
color: white;
}

th,
td {
padding: 10px;
}

thead {
position: sticky;
top: 0;
}

.news-section {
display: grid;
grid-template-rows: ""
}

.filter-controls-wrapper {
margin-bottom: 16px;
display: flex;
justify-content: flex-end;
}


input[type="search"] {
border: 1px solid transparent;
border-radius: 2rem;
padding-bottom: 5px;
padding-left: 16px;
padding-right: 16px;
padding-top: 16px;
padding-bottom: 16px;
transition: 0.2s border-color ease-in-out;
}

* {
font-family: 'Source Sans Pro', sans-serif;
}

label {
font-size: 24px;
}

input[type="search"]:focus {
border-color: navy;

}


/*PAgination here*/
span .paginate_button {
width: 80px ;
height: 25px ;
margin: 0 10px ;
border-radius: 999px ;
background: crimson ;
cursor: pointer;
box-shadow:
0 5px 5px -5px rgba(crimson, 0.15),
0 10px 10px -5px rgba(crimson, 0.15),
0 15px 15px -5px rgba(crimson, 0.15),
0 20px 20px -5px rgba(crimson, 0.15) ;
transition: 0.25s ease;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
padding: 10px;
}

[id*="toggle-"]:hover{
width: 120px;
}

.dataTables_paginate {
margin-top: 16px;
display: flex;
justify-content: center;
align-items: center;
}
.dataTables_paginate span{
display: flex;
justify-content: center;
align-items: center;
}
<table id="news">
<thead>
<tr>
<th>Titulo</th>
<th>Opciones</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>
<a href="#"><span class="fa fa-edit"></span></a>
<a href="#"><span class="fa fa-eye-slash"></span></a>
<a href="#"><span class="fa fa-trash"></span></a>
</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>
<a href="#"><span class="fa fa-edit"></span></a>
<a href="#"><span class="fa fa-eye-slash"></span></a>
<a href="#"><span class="fa fa-trash"></span></a>
</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr> <tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>41</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>55</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>6</td>
</tr>

<tr>
<td>Tiger Nixon</td>
<td>2</td>
</tr>

</tbody>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/rowreorder/1.2.6/js/dataTables.rowReorder.min.js"></script>

关于javascript - ¿如何防止在单击分页按钮或标题列时重置 JQuery 数据表样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com