0

誰かこの Javascript コードを修正してください。

このスクリプトは実際に URL パラメータを読み取り、パラメータに応じて表の行を表示/非表示にします。

このスクリプトは Sack Overflow で見つけましたが、Dreamweaver で試してみると、うまくいきません。

誰かがスクリプトを調べて、何が間違っているかを修正してください....

スクリプトは次のとおりです。ここには 2 つのページがあります...

最初は次のFirst_page.htmlとおりです。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<a href="Second_page.html?showid=tblRow14">First Row</a><br />
<a href="Second_page.html?showid=tblRow46">Second Row</a><br />
<a href="Second_page.html?showid=tblRow30">Third Row</a><br />
</body>
</html>

そしてSecond_page.html以下の通りです:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<head>
<style>
#theTable>tbody>tr { display: none; } //hide rows by default
</style>
<script type="text/javascript">
function getUrlVar(varName) { //returns empty string if variable name not found in URL
  if (!varName) return ''; //no variable name specified. exit and return empty string

  varName = varName.toLowerCase(); //convert to lowercase
  var params = location.search; //get URL

  if (params == '') return ''; //no variables at all. exit and return empty string

  var vars = params.split('?')[1].split('&'); //get list of variable+value strings

  for (var i = 0; i < vars.length; i++) { //check each variable
   var varPair = vars[i].split('='); //split variable and its value

   if (varPair.length > 1) { //has "=" separator

     if (varPair[0].toLowerCase() == varName) { //same variable name?
       return varPair[1]; //found variable. exit and return its value
     } //else: check next variable, if any

   } //else: is not an array. i.e.: invalid URL variable+value format. ignore it
  }
  return ''; //no matching variable found. exit and return empty string
}

function show() {
  var value = getUrlVar('showid'); //get variable value
  if (!value) return; //variable not found
  if (parseInt(value) == NaN) return; //value is not a number

  var row = document.getElementById('tblRow' + value); //get the element by ID name
  if (!row) return; //element not found

  row.style.display = 'inherit'; //set element display style to inherited value (which is visible by default)
}
</script>
</head>

<body onLoad="show();">
<table id="theTable">
  <tr id="tblRow14"><td>row ID 14</td></tr>
  <tr id="tblRow46"><td>row ID 46</td></tr>
  <tr id="tblRow30"><td>row ID 30</td></tr>
</table>
</body>
</html>

それを修正するか、これと同じように機能する新しいJavascriptコードを見つけるのを手伝ってください...

これで私を助けてください。

4

2 に答える 2

0

「値」変数にはすでに の ID があります。したがって、次の行を変更するだけです。

var row = document.getElementById(value); 

そして、それはうまくいきます。

于 2012-08-29T16:45:58.547 に答える
0

URL のパラメーター showid は整数である必要があります。First_page.html のリンクを変更します。

<a href="Second_page.html?showid=14">First Row</a><br />
<a href="Second_page.html?showid=46">Second Row</a><br />
<a href="Second_page.html?showid=30">Third Row</a><br />
于 2012-08-29T16:48:10.633 に答える