简介
我正在实现一个类似于任何浏览器中的 Ctrl+F 的搜索引擎,基本上当我单击旁边的按钮时,浏览器必须滚动到下一个具有名为突出显示的类的元素。
我已经有这个方法了。
function scrollToElement(selector, time, verticalOffset) {
time = typeof (time) != 'undefined' ? time : 500;
verticalOffset = typeof (verticalOffset) != 'undefined' ? verticalOffset : 0;
element = $(selector);
offset = element.offset();
offsetTop = offset.top + verticalOffset;
$('html, body').animate({
scrollTop: offsetTop
}, time);
}
这个:
var highlights = $('.highlight');
返回以下数组,
我需要找到一种方法来滚动到数组中所需的跨度,每个跨度元素内都有类似的内容。
accessKey: ""
attributes: NamedNodeMap
baseURI: "http://localhost:51939/FBDefault.aspx"
childElementCount: 0
childNodes: NodeList[1]
children: HTMLCollection[0]
classList: DOMTokenList[1]
className: "highlight"
clientHeight: 0
clientLeft: 0
clientTop: 0
clientWidth: 0
contentEditable: "inherit"
dataset: DOMStringMap
dir: ""
draggable: false
firstChild: text
firstElementChild: null
hidden: false
id: ""
innerHTML: "al"
innerText: "al"
isContentEditable: false
lang: ""
lastChild: text
lastElementChild: null
localName: "span"
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: span.caret
nextSibling: text
nodeName: "SPAN"
nodeType: 1
nodeValue: null
offsetHeight: 19
offsetLeft: 43
offsetParent: a.dropdown-toggle
offsetTop: 15
offsetWidth: 13
outerHTML: "<span class="highlight">al</span>"
outerText: "al"
ownerDocument: document
parentElement: a.dropdown-toggle
parentNode: a.dropdown-toggle
prefix: null
previousElementSibling: i.fa.fa-calculator.margen
previousSibling: text
scrollHeight: 0
scrollLeft: 0
scrollTop: 0
scrollWidth: 0
shadowRoot: null
spellcheck: true
style: CSSStyleDeclaration
tabIndex: -1
tagName: "SPAN"
textContent: "al"
title: ""
translate: true
webkitdropzone: ""
__proto__: HTMLSpanElement
最佳答案
要从 jQuery 搜索中获取第 n:th 元素,您可以使用 .get()
,因此您应该能够使用您所拥有的内容
var span = $(".highlight").get(spanIndex);
scrollToElement(span, time, verticalOffset);
并且您可以使用某些变量跟踪选择中的当前span
。
关于javascript - 滚动到 jquery 数组中的下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654240/