Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在<;部门>;滚动到给定字符串的第一个匹配项_Javascript_Jquery_Scroll - Fatal编程技术网

Javascript 在<;部门>;滚动到给定字符串的第一个匹配项

Javascript 在<;部门>;滚动到给定字符串的第一个匹配项,javascript,jquery,scroll,Javascript,Jquery,Scroll,在我的div标记中,有许多行非常长的文本,div元素当前是可滚动的。我想做的是找到特定字符串的第一个匹配项,并自动滚动到包含该字符串的行。没有换行符或任何类型的字符串分隔符,我可以使用它们来近似滚动位置 我想我可以这样做: var max_chars_per_line = approximated max # of chars that can fit into one line; var font_height = approximated font height in px; var nee

在我的div标记中,有许多行非常长的文本,div元素当前是可滚动的。我想做的是找到特定字符串的第一个匹配项,并自动滚动到包含该字符串的行。没有换行符或任何类型的字符串分隔符,我可以使用它们来近似滚动位置

我想我可以这样做:

var max_chars_per_line = approximated max # of chars that can fit into one line;
var font_height = approximated font height in px;
var needle = string to look for;
var haystack = content in the <div> tag;
var index = haystack.indexOf(needle);
var num_lines_to_skip = index / max_chars_per_line;
my_div_tag.scrollTo(num_lines_to_skip * font_height);
var max_chars_per_line=可放入一行的近似最大字符数;
var font_height=近似的字体高度(以像素为单位);
var针=要查找的字符串;
var haystack=标记中的内容;
var指数=干草堆指数(针);
var num_lines_to_skip=每行的索引/最大字符数;
my_div_tag.scrollTo(num_lines_to_skip*font_height);

我不禁想知道是否有更好的(javascript/jQuery)解决方案。我自己的尽职调查没有发现任何问题

您不能执行类似于使用以下Java类型代码的操作吗:

  var lineToTest = getLine(textArray);

  if(lineToTest.contains(testingLine){
   lineIndex = getLineNumber(textArray-1); //since it already accessed the line it'll have to access the next and subtract 1. }

然后使用任何突出显示技术来突出显示线条。显然,您必须创建getLine、getLineNumber函数。除非有一些很酷的jQuery函数(可能就是这样)

如果您的所有文本都在一个div中,那么类似的解决方案是有效的

var goto = function(str){

    var content = $('#content').html();
    $('#content').html( content.replace(str,'<a name="goto">'+str+'</a>') );
    window.location = '#goto';
}

goto('Whatever your string is here');
var goto=函数(str){
var content=$('#content').html();
$(“#content').html(content.replace(str)”

Lorem ipsum…大量文本…洋葱

jQ:

var针='洋葱';

$(“#text”).html($(“#text”).html().replace(pinder),

Ha,我知道你怎么做,但在JSFIDLE中需要一点时间。这不是java程序,他们说有一个文本字符串,所以getLine()函数帮不了忙。非常有用。谢谢!FIDLE链接断了。
<p id="text">Lorem ipsum ... lots of text.... onion</p>
var needle = 'onion';
$('#text').html($('#text').html().replace(needle, '<a id="imhere"></a>'+needle));
$('html,body').animate({scrollTop: $('#imhere').offset().top​}, 500);​​​​​​​​​​​​