ดังนั้นฉันจึงเปิดและปิดฟังก์ชันการค้นหามาระยะหนึ่งแล้ว Tim Down ช่วยได้ ก> ฉันมากและให้โค้ดง่ายๆ แก่ฉันเพื่อค้นหาข้อความบางอย่างในหน้านั้น
ตอนนี้ฉันกำลังพยายามแก้ไขโค้ดเพื่อค้นหาตามหมวดหมู่ ฉันมีตาราง HTML แบ่งออกเป็น divs ดังนี้
<form id="f1" name="f1" action="javascript:void(0)" onsubmit="searchpage()" >
<input id="t1" type="text" name="t1" />
<select name="category" id="category">
<option>all</option>
<option value="name">Title</option>
<option value="author">Author</option>
<option value="year">Year</option>
<option value="publisher">Publisher</option>
</select>
<input id="button" type="submit" value="SEARCH" name="b1" onclick="searchpage()" />
</form>
<tr>
<td>
<div id="name">
<div id="title">
title 1
</div>
extra content
</div>
</td>
<td>
<div id="author">
author 1
</div>
</td>
<td>
<div id="year">
2000
</div>
</td>
<td>
<div id="publisher">
publisher 1
</div>
</td>
<td>
<div id="notes">
notes 1
</div>
</td>
</tr>
<!--etc...-->
Javascript ของฉันเป็นดังนี้
/*these two variables select the value attribute of each option in the dropbox*/
var cat=document.getElementById("category").selectedIndex;
var catid=document.getElementsByTagName("option")[cat].value;
function doSearch(text) {
var sel;
if (window.find && window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount > 0) {
sel.collapseToEnd();
}
window.find(text);
} else if (document.selection && document.body.createTextRange) {
sel = document.body.selection;
var textRange;
if (sel.type == "Text") {
textRange = sel.createRange();
textRange.collapse(false);
} else {
textRange = document.body.createTextRange();
textRange.collapse(true);
}
if (textRange.findText(text)) {
textRange.select();
}
}
}
function searchpage() {
doSearch(document.getElementById("t1").value);
}
อย่างที่คุณเห็น แอตทริบิวต์ค่าจะตรงกับรหัสใน div ฉันต้องการให้จาวาสคริปต์ค้นหาเฉพาะ div เฉพาะเมื่อเลือกตัวเลือกที่เกี่ยวข้อง ไม่แน่ใจว่าจะไปจากที่นี่ได้อย่างไร
ฉันสามารถใช้คุณสมบัติ "ใน" เพื่อกำหนดว่าฉันต้องการค้นหาในคลาสหรือรหัสที่ต้องการได้หรือไม่ บางสิ่งบางอย่างตามแนวของ
doSearch(document.getElementById("t1").value in document.getElementsByClassName("name"));}