gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:53:36 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com