gpt4 book ai didi

javascript - 为什么在 Fiddle 中工作的代码不能在在线 html 编辑器中工作?

转载 作者:行者123 更新时间:2023-12-03 03:41:15 25 4
gpt4 key购买 nike

我知道这是一个不相关的问题,但我遇到了一些困难。我在 fiddle https://jsfiddle.net/ew5y6pd1/4/ 找到了一个代码它工作得很好,但是当我复制并放置在线 html 编辑器时,数据不会附加。下面显示了我复制到在线 html 编辑器的代码。我错过了什么图书馆吗?

<!Doctype html>
<html>
<head>

<style>
th, td { border: 1px solid black;}
</style>


<script>
$(document).ready(function() {

$("#add").on('click', function() {
var user = {
Id: '',
Name: ''
}
var row = $('<tr/>');
user.Id = $("#id").val();
user.Name = $("#Name").val();



row.append($('<td/>').text(user.Id));
row.append($('<td/>').text(user.Name));

$("#reservations tbody").append(row);
});

$('#sort').on('click', function(){
var rows = $('#reservations tbody tr').get();

rows.sort(function(a, b) {

var A = $(a).children('td').eq(0).text().toUpperCase();
var B = $(b).children('td').eq(0).text().toUpperCase();

if(A < B) {
return -1;
}

if(A > B) {
return 1;
}

return 0;

});

$.each(rows, function(index, row) {
$('#reservations').children('tbody').append(row);
});
})
});

</script>


</head>
<body>



<input type="text" id="id" />
<input type="text" id="Name" />
<input type="button" id="add" value="Add" />
<br />
<input type="button" id="sort" value="sort" />

<table id="reservations">
<thead>
<tr>
<th> Id </th>
<th> Name </th>
</tr>
</thead>
<tbody>
</tbody>
</table>





</body>
</html>

最佳答案

Screenshot of JS Fiddle

JS Fiddle 实例加载 the jQuery library它提供了 $功能。

您需要下载该库(或找到托管版本的 URL)并包含 <script>在运行依赖于它的脚本之前加载它的元素。

关于javascript - 为什么在 Fiddle 中工作的代码不能在在线 html 编辑器中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45615225/

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