我正在编写搜索 HTML 的程序。
我将搜索到的关键字包裹在带有“find”类的span标签中,如下所示:
<b><span class="find">A</span>
<i>B</i>C</b>DDD<b><span class="find">A</span></span><i>B</i>C</b>
当单击“下一个”或“上一个”按钮时,我想从当前选择点移动到最近的下一个/上一个跨度标记(我从 window.getSelection()
获得)。
如果可以使用"jQuery(selection.focusNode).next('.find');"
,我会简单解决它,但它是无效的。有什么好的方法吗?
最佳答案
也许你可以做类似的事情:
var foo = {
spans: $('span.find'),
pos: -1,
getSpan: function(inc) {
var pos = this.pos + inc;
if (pos >= 0 && pos < this.spans.length) {
this.pos = pos;
return this.spans[this.pos];
}
return undefined;
},
next: function() {
var span = this.getSpan(1);
// do what you have to do with it
},
prev: function() {
var span = this.getSpan(-1);
// do what you have to do with it
},
}
然后将next和prev“连接”到您的按钮。
这是您正在寻找的吗?
关于javascript - 如何从当前插入符移动到具有特定类名的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18889982/