javascript - 从浏览器运行 html 中的 javascript

标签 javascript html

我在 jsfiddle 有一个可以工作的 javascript从这里post并希望使用我的浏览器在本地运行。

我不太确定还需要在 HTML 中添加什么才能使其正常运行。我读了很多帖子,但仍然不知道如何解决它。

下面是我所做的,但运行代码片段时出现错误,有人可以帮忙吗?谢谢。

$(document).ready(function myFunction() {

  // Declare variables 
  var input = document.getElementById("myInput");
  var filter = input.value.toUpperCase();
  var table = document.getElementById("myTable");
  var trs = table.tBodies[0].getElementsByTagName("tr");

  // Loop through first tbody's rows
  for (var i = 0; i < trs.length; i++) {

    // define the row's cells
    var tds = trs[i].getElementsByTagName("td");

    // hide the row
    trs[i].style.display = "none";

    // loop through row cells
    for (var i2 = 0; i2 < tds.length; i2++) {

      // if there's a match
      if (tds[i2].innerHTML.toUpperCase().indexOf(filter) > -1) {

        // show the row
        trs[i].style.display = "";

        // skip to the next row
        continue;

      }
    }
  }

});
#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,
#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable th {
  width: 20%;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,
#myTable tr:hover {
  background-color: #f1f1f1;
}
<!DOCTYPE html>
<html>

<head>

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax"></script>

</head>

<body>
  <p><input id="myInput" type="text" placeholder="Search for names.." /></p>
  <table id="myTable">
    <thead>
      <tr class="header">
        <th>Date</th>
        <th>Home</th>
        <th>Time</th>
        <th>Away</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>08/01/2018</td>
        <td>SPAIN</td>
        <td>16:30 ET</td>
        <td>USA</td>
        <td>BARCELONA</td>
      </tr>
      <tr>
        <td>08/02/2018</td>
        <td>BOLIVIA</td>
        <td>18:30 ET</td>
        <td>PORTUAL</td>
        <td>MADRID</td>
      </tr>
      <tr>
        <td>08/03/2018</td>
        <td>PUERTO RICO</td>
        <td>18:30 ET</td>
        <td>CANADA</td>
        <td>CHICAGO</td>
      </tr>
      <tr>
        <td>08/04/2018</td>
        <td>MEXICO</td>
        <td>19:30 ET</td>
        <td>ENGLAND</td>
        <td>LONDON</td>
      </tr>
    </tbody>
  </table>
</body>


</html>

最佳答案

首先,这里不需要 jQuery。我对你的代码做了两处更改,它起作用了

  1. 删除了 $(document).ready( 和结束 )
  2. 在输入中添加了 onkeyup="myFunction()"

请检查下面的代码。

#myInput {
      background-image: url('/css/searchicon.png');
      background-position: 10px 12px;
      background-repeat: no-repeat;
      width: 100%;
      font-size: 16px;
      padding: 12px 20px 12px 40px;
      border: 1px solid #ddd;
      margin-bottom: 12px;
    }
    
    #myTable {
      border-collapse: collapse;
      width: 100%;
      border: 1px solid #ddd;
      font-size: 18px;
    }
    
    #myTable th,
    #myTable td {
      text-align: left;
      padding: 12px;
    }
    
    #myTable th {
      width: 20%;
    }
    
    #myTable tr {
      border-bottom: 1px solid #ddd;
    }
    
    #myTable tr.header,
    #myTable tr:hover {
      background-color: #f1f1f1;
    }
<script type="text/javascript" src="https://ajax.googleapis.com/ajax"></script>


<body>
  <p><input id="myInput" onkeyup="myFunction()" type="text" placeholder="Search for names.." /></p>
  <table id="myTable">
    <thead>
      <tr class="header">
        <th>Date</th>
        <th>Home</th>
        <th>Time</th>
        <th>Away</th>
        <th>City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>08/01/2018</td>
        <td>SPAIN</td>
        <td>16:30 ET</td>
        <td>USA</td>
        <td>BARCELONA</td>
      </tr>
      <tr>
        <td>08/02/2018</td>
        <td>BOLIVIA</td>
        <td>18:30 ET</td>
        <td>PORTUAL</td>
        <td>MADRID</td>
      </tr>
      <tr>
        <td>08/03/2018</td>
        <td>PUERTO RICO</td>
        <td>18:30 ET</td>
        <td>CANADA</td>
        <td>CHICAGO</td>
      </tr>
      <tr>
        <td>08/04/2018</td>
        <td>MEXICO</td>
        <td>19:30 ET</td>
        <td>ENGLAND</td>
        <td>LONDON</td>
      </tr>
    </tbody>
  </table>
</body>
<script>
function myFunction() {

    // Declare variables 
    var input = document.getElementById("myInput");
    var filter = input.value.toUpperCase();
    var table = document.getElementById("myTable");
    var trs = table.tBodies[0].getElementsByTagName("tr");

    // Loop through first tbody's rows
    for (var i = 0; i < trs.length; i++) {

      // define the row's cells
      var tds = trs[i].getElementsByTagName("td");

      // hide the row
      trs[i].style.display = "none";

      // loop through row cells
      for (var i2 = 0; i2 < tds.length; i2++) {

        // if there's a match
        if (tds[i2].innerHTML.toUpperCase().indexOf(filter) > -1) {

          // show the row
          trs[i].style.display = "";

          // skip to the next row
          continue;

        }
      }
    }

  };
  </script>

关于javascript - 从浏览器运行 html 中的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66452245/

相关文章:

javascript - 在 Javascript 中将多个字符串添加为数字

javascript - 使用 Webpack 和 Laravel Mix 解析动态模块

javascript - 使用 Jquery 选择多个元素,但使用 addClass() 仅向其中一个元素添加一个类

javascript - 将 JS 转换为自动加载

php - 没有错误,但是我的记录没有被添加到我的数据库中

javascript - 将字符串列表从 Javascript 传递到 asp.net 代码隐藏

javascript - 如何从 MYSQL 获取单个值并将其保存到 JavaScript 变量中?

javascript - Google 应用程序脚本 .toUpperCase 跳过数字

html - 我可以在 IOS 5 的一个 UIWebView 中有两种不同的样式吗

javascript - 从div中取出文本,插入文本区域,进行编辑,文本区域不显示?