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 の内容を検索するなど、私がやりたいことを既に実行しているスクリプトはありますか?