实现一个 javascript 函数来在 div 中搜索,获取搜索的文本范围

发布于 2024-12-06 10:48:47 字数 2230 浏览 0 评论 0原文

如何获取文本范围来搜索 div(或表单)?是否有可用的脚本或搜索 div 文本的 jquery 函数?

我使用以下代码将表单附加到 div:

    $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');

我想要一个搜索函数,该函数只会在该 div 中查找字符串。我这样指定文本范围。

txt = window.document.body.getelementbyid(card_id).createTextRange();

搜索功能是我在网上找到的,我正在尝试更新以搜索 div 而不是整个页。页面上会有几个 div,我希望搜索针对每个 div。我从 search_links(card_id); 调用该函数。

function search_links (card_id, form) {
    var search_str = document.frm_search.linkcard_search_string.value;
    /* alert('search_links '+search_str); */
    return search_linkcard(search_str, card_id);
}

var IE4 = (document.all);
var n   = 0;
function search_linkcard(str, card_id) {

  alert (card_id + ' ' + str);
  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (IE4) {
    txt = window.document.body.getelementbyid(card_id).createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        search_linkcard(str, card_id);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  }

  return false;
}

我的具体问题是问题开头的那些问题:如何指定 div 的文本范围?我的语法正确吗?是否有脚本已经可以执行我想要的操作,即搜索特定 div 的内容?

How do I get the textrange to do a search for a div (or a form)? Are there scripts already available, or jquery functions that search the text of a div?

I append a form to a div with this code:

    $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');

I'd like to have a search function that will only look for a string in that div. I specify the text range like this.

txt = window.document.body.getelementbyid(card_id).createTextRange();

The search function is one that I found on the net and that I am trying to update to search the div instead of the entire page. There will be several divs on the page and I want the search to be specific to each. I call that function from search_links(card_id);.

function search_links (card_id, form) {
    var search_str = document.frm_search.linkcard_search_string.value;
    /* alert('search_links '+search_str); */
    return search_linkcard(search_str, card_id);
}

var IE4 = (document.all);
var n   = 0;
function search_linkcard(str, card_id) {

  alert (card_id + ' ' + str);
  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (IE4) {
    txt = window.document.body.getelementbyid(card_id).createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        search_linkcard(str, card_id);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  }

  return false;
}

My specific questions are those at the beginning of the question: How do I specify a text range for the div? Is the syntax I have right? Are there scripts that already do what I want, i.e. search the contents of a specific div?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-12-13 10:48:48

是否使用 :contains 进行搜索。一次没有进行一场比赛。突出显示所有匹配项。

// Open search
function open_search(card_id) {

    $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form" onclick="clear_search(\''+card_id+'\', this.form);"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');

    var frm_elements = frm_search_link.elements;
    for(i=0; i<frm_elements.length; i++) {
        field_type = frm_elements[i].type.toLowerCase();
        switch (field_type)
        {
            case "text":
                frm_elements[i].value = ""; 
                break;

            default:
                break;
        }
    }

}

// Close search
function close_search(card_id, form) {
    $('form.frm_search_links', $('#'+card_id)).remove();
    var card_select = '#'+card_id;
    $('.link',$(card_select)).removeClass('search_results');        
}

// Search links
function search_links (card_id, form) {
    var search_str = document.frm_search.linkcard_search_string.value;
    var search_select = '.link:contains('+search_str+')';
    var card_select = '#'+card_id;
    var result = $(search_select,$(card_select)).addClass('search_results');
    if (result.length == 0 || result.length == null) document.frm_search.linkcard_search_string.value = 'Not found.';
}

// Clear search
function clear_search (card_id, form) {
    document.frm_search.linkcard_search_string.value = '';
    var card_select = '#'+card_id;
    $('.link',$(card_select)).removeClass('search_results');        
}

Did the search with :contains. Did not do one match at a time. Highlighted all matches.

// Open search
function open_search(card_id) {

    $('#'+card_id).append('<form id="frm_search" name="frm_search" class="editableToolbar frm_search_links"> <input type="text" placeholder="Type a string..." name="linkcard_search_string" class="txt_form" onclick="clear_search(\''+card_id+'\', this.form);"> <a href="#" title="Search" class="ico_search btn_form" onClick="search_links(\''+card_id+'\', this.form); "></a> <a href="#" title="Close" class="ico_delete btn_form" onClick="close_search(\''+card_id+'\', this.form); "></a> </form>');

    var frm_elements = frm_search_link.elements;
    for(i=0; i<frm_elements.length; i++) {
        field_type = frm_elements[i].type.toLowerCase();
        switch (field_type)
        {
            case "text":
                frm_elements[i].value = ""; 
                break;

            default:
                break;
        }
    }

}

// Close search
function close_search(card_id, form) {
    $('form.frm_search_links', $('#'+card_id)).remove();
    var card_select = '#'+card_id;
    $('.link',$(card_select)).removeClass('search_results');        
}

// Search links
function search_links (card_id, form) {
    var search_str = document.frm_search.linkcard_search_string.value;
    var search_select = '.link:contains('+search_str+')';
    var card_select = '#'+card_id;
    var result = $(search_select,$(card_select)).addClass('search_results');
    if (result.length == 0 || result.length == null) document.frm_search.linkcard_search_string.value = 'Not found.';
}

// Clear search
function clear_search (card_id, form) {
    document.frm_search.linkcard_search_string.value = '';
    var card_select = '#'+card_id;
    $('.link',$(card_select)).removeClass('search_results');        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文