实现一个javascript函数来搜索div,获取搜索的textrange

时间:2011-09-24 16:27:43

标签: javascript jquery search textrange

如何让textrange搜索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,我希望搜索特定于每个。我从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的内容吗?

1 个答案:

答案 0 :(得分:0)

搜索是否包含:。一次没做一场比赛。突出显示所有比赛。

// 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');        
}