0
我有一段代碼可以通過表格搜索某個特定的詞組。一旦找到該短語,它就會將當前單元格的innerHTML返回到頁面頂部附近的div。Javascript搜索問題
我遇到的問題是這樣的:
代碼完美的作品,除非被搜索的短語包含任何特殊字符(括號和&號等)。我試圖修改從「Western & Central"
到"Western & Central"
的詞語,但不幸的是,代碼仍無法找到這句話
繼承人的代碼:
// Function for searching tables for specific map/device combination.
function findMap(map, device)
{
var flag = false; // to check if the map has been found yet
// Add <strong> tags to map string so it is easier to search for
var mapstring = "<strong>" + map;
var map_regex = new RegExp(mapstring,'i');
// create array of tables
var tables = document.getElementsByTagName('table');
for (var t = 2; t < tables.length; t++)
{
// create array of rows
var tablerows = tables[t].rows;
for (var r = 2; r < tablerows.length; r++)
{
currentRow = tablerows[r];
// Check if current row contains map name
if (currentRow.innerHTML.match(map_regex))
{
var tablecells = currentRow.cells;
for (var c = 1; c < tablecells.length; c++)
{
currentCell = tablecells[c];
// Check if current cell contains device name
if (currentCell.innerHTML.match(device))
{
document.getElementById("boxy2").innerHTML = currentCell.innerHTML;
flag = true;
}
if (flag == true) return;
}
if (flag == true) return;
// search for x20 if 920 was not found etc
if (device == "910")
device = "x10";
else if (device == "920")
device = "x20";
else if (device == "930")
device = "x30";
else if (device == "940")
device = "x40";
else if (device == "950")
device = "x50";
// search cells again
for (var c = 1; c < tablecells.length; c++)
{
currentCell = tablecells[c];
if (currentCell.innerHTML.match(device))
{
document.getElementById("boxy2").innerHTML = currentCell.innerHTML;
flag = true;
}
if (flag == true) return;
}
if (flag == true) return;
}
if (flag == true) return; else { document.getElementById("boxy2").innerHTML="Map not available on this device."; }
}
}
}
這件事的任何幫助將是非常感謝!
只是爲了澄清,應該從「西部科技中心」到「西方& amp ;中央」說(沒有空格)。謝謝。 – MattDrewery 2011-02-15 10:31:01