我有很多行的表。所有行都有类参数。 我想添加按钮“向下”,当我单击按钮时,我想向下滚动到类“ error”的第一个元素,当我再次单击按钮“向下”时,我想向下滚动到第二类,元素是“ error”
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>
此代码仅发现第一个元素良好。
答案 0 :(得分:0)
您可以为此使用计数器变量
脚本:
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>