简体   繁体   English

我如何获取“ indexOf”元素 <p> ? (jquery)

[英]How can I get the “indexOf” an element in a <p>? (jquery)

I just want to get the index position of tagged elements(marks) inside a paragraph. 我只想获取段落内标记元素(标记)的索引位置。 ex: 例如:

<p>
   Lorem ipsum dolor ipsum sit <mark>amet</mark>
   consectetur adipisicing elit, sed <mark>amet</mark> 
</p>

I'm trying to emulate the " ctrl+f " function in a div. 我正在尝试在div中模拟“ ctrl + f ”功能。 So im using a plugin called textselect to select/highlight my search result with this: 所以即时通讯使用一个名为textselect的插件来选择/突出显示我的搜索结果:

var startPosition = textHolder.indexOf(textToSelect);
var endPosition = startPosition + textToSelect.length;

$.textSelect('setRange', {
                        start: startPosition,
                        startElement: $("p"),
                        end : endPosition,
                        endElement : $("p")
                    });

but i don't know how can i get the startposition ... 但是我不知道如何获得起点 ...

I tried this: 我尝试了这个:

var textHolder = $('div').find('p').contents("mark").eq(0);

$('div').find('p').html().indexOf(textHolder.html())

and returns 34 this would be the startposition of the first mark but if change the textholder to .eq(1) returns 34 again. 并返回34,这将是第一个标记的起始位置,但是如果将文本.eq(1)更改为.eq(1)则会再次返回34。 How can I get the indexOf of each mark substring? 如何获得每个标记子字符串的indexOf

I believe this is basically what you are looking for: 我相信这基本上是您要寻找的:

var paragraph = $("#paragraph").html();
var startIndex = paragraph.indexOf("<mark>");
var endIndex = paragraph.indexOf("</mark>");

If you want to do this dynamically just put it within a loop a substring from the endIndex each time. 如果您想动态地执行此操作,则只需将其每次放入endIndex的子字符串中即可。 Something like this: 像这样:

var paragraph = $("#paragraph").html();
var lastIndexFound = false;
while(!lastIndexFound)
{
    var startPostition = paragraph.indexOf("<mark>");
    var endPosition = paragraph.indexOf("</mark>");
    if (endPosition == -1)
    {
        lastIndexFound = true;
        break;
    }
    $.textSelect('setRange', {
                    start: startPosition,
                    startElement: $("p"),
                    end : endPosition,
                    endElement : $("p")
                });
    paragraph = paragraph.substring(endPosition, paragraph.length);

}

I would suggest adding an id to your paragraph as well as to your 'marked' tags and then obtaining your text as a variable like so: 我建议您在段落和“标记”标签中添加一个id,然后像这样获取文本作为变量:

var paragraph = document.getElementById("paragraph_id").innerHTML;

You can then get the starting positions of your substrings using this: 然后,您可以使用以下命令获取子字符串的起始位置:

var n1 = paragraph.indexOf('<marked id="m1">');
var n2 = paragraph.indexOf('<marked id="m2">');

If you need to get your ending indexes, you can save other strings from your 'marked' tags similar as described before and add the m1_string_name.length property to your n1 value and m2_string_name.length property to your n2 value. 如果需要获取结束索引,则可以像前面所述一样从“标记”标记中保存其他字符串,并将m1_string_name.length属性添加到n1值,并将m2_string_name.length属性添加到n2值。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM