javascript - 向下滚动具有相同行类名称的所有表格元素
问题描述
我有很多行的表。所有行都有类参数。我想添加按钮“向下”,当我单击按钮时,我想向下滚动到类“错误”的第一个元素,当我再次单击按钮“向下”时,我想向下滚动到第二个元素,类为“错误”等等。
JS
<script>
var $currentElement = $(".error").first();
$("#down").click(function () {
$currentElement = $currentElement.next();
$('html, body').animate({
scrollTop: $currentElement.offset().top
}, 1000);
return false;
});
$("#up").click(function () {
var currentElement = $currentElement.prev();
$('html, body').animate({
scrollTop: $(currentElement).offset().top
}, 1000);
return false;
});
桌子
<table id="example" class="table table-bordered table-dark">
<thead>
<tr>
<!--<th scope="col"><small>Test ID</small></th>-->
<th scope="col"><small>Name</small></th>
<th scope="col"><small>Test</small></th>
<th scope="col"><small>Start</small></th>
<th scope="col"><small>End</small></th>
</tr>
</thead>
<tbody>
{% for element in record %}
{% if element.1 == 'Failed' or element.1 == 'Error' %}
<tr class="error">
<td><small>{{ element.0 }}</small></td>
<td><small><a href="{% url 'build_details' iteration element.6 %}" class="text-white">{{ element.6 }}</a></small></td>
<td><small>{{ element.4|slice:"11:22" }}</small></td>
<td><small>{{ element.5|slice:"11:22" }}</small></td>
</tr>
{% elif element.1 == 'Inconclusive' or element.1 == 'Warning' or element.1 == 'Timeout' or element.1 == 'Aborted' or element.1 == 'Notrunnable' or element.1 == 'Notexecuted' %}
<tr class="bg-warning">
<td><small>{{ element.0 }}</small></td>
<td><small><a href="{% url 'build_details' iteration element.6 %}" class="text-white">{{ element.6 }}</a></small></td>
<td><small>{{ element.4|slice:"11:22" }}</small></td>
<td><small>{{ element.5|slice:"11:22" }}</small></td>
</tr>
{% elif element.1 == 'Passed' %}
<tr class="bg-success">
<td><small>{{ element.0 }}</small></td>
<td><small><a href="{% url 'build_details' iteration element.6 %}" class="text-white">{{ element.6 }}</a></small></td>
<td><small>{{ element.4|slice:"11:22" }}</small></td>
<td><small>{{ element.5|slice:"11:22" }}</small></td>
</tr>
{% else %}
<tr>
<td><small>{{ element.0 }}</small></td>
<td><small><a href="{% url 'build_details' iteration element.6 %}" class="text-white">{{ element.6 }}</a></small></td>
<td><small>{{ element.4|slice:"11:22" }}</small></td>
<td><small>{{ element.5|slice:"11:22" }}</small></td>
</tr>
{% endif %}
{% endfor %}
</tbody>
<tfoot>
<tr>
<!--<th scope="col"><small>Test ID</small></th>-->
<th scope="col"><small>Name</small></th>
<th scope="col"><small>Test</small></th>
<th scope="col"><small>Start</small></th>
<th scope="col"><small>End</small></th>
</tr>
</tfoot>
</table>
此代码仅发现第一个元素很好。
解决方案
您可以为此目的使用计数器变量,
脚本:
var elemCounter = 0;
function onClick(){
var height = $($('.error')[elemCounter]).offset().top;
var parent = $(".error").parent();
parent.scrollTop(height);
elemCounter ++;
if($(".error").length < elemCounter)
elemCounter = 0;
}
// JQuery Selector for clicking the button ..
$(document).on('click', 'scrollBtn' ,function(){
var height = $($('.error')[elemCounter]).offset().top;
$(".error").parent().scrollTop(height);
elemCounter ++;
if($(".error").length < elemCounter)
elemCounter = 0;
}
// Use one of the above function
您也可以使用class/id 选择器来选择父级,这里您没有提到父级,所以我使用 Jquery 选择了它。
您可以在单击某些按钮时调用您的函数,例如:
HTML:
<button id="scrollBtn" onclick="onClick()">Click</button>
推荐阅读
- javascript - Javascript:用 html 字符串中的正则表达式替换图像名称
- angular - 角度拖放:如何克隆元素,拖动新克隆的元素
- r - 使用 ggragplot 制作刻面
- c++ - FreeType 库不与教程代码一起显示
- python - Pandas:索引 0 超出轴 0 的范围,大小为 0
- java - Edittext 在上述 android N 的对话框中无法接收来自键盘的输入
- angular - 如何在单个构建命令中在 Angular 7 项目中构建多个应用程序?
- html - 如何配置 webpack 以使用预构建的 svg sprite?
- yii2 - Yii2 DepDrop Kartik 小部件
- batch-file - 如何使用 WMIC 在所有物理端口(包括断开连接的媒体端口)上强制 DHCP