如何使 HTML 网站具有交互性,以便当用户单击删除按钮时它会为所有用户更改

我用 HTML 制作了一个表格,并添加了一个删除按钮,以便它删除表格中最后记录的行。

删除按钮有效,但当我刷新页面时,编辑内容消失了,一切都回到了原始状态。

我怎样才能让它在用户编辑页面时永久改变?

这是一个演示:http: //jsfiddle.net/objcLfxd/#&togetherjs=9ai74rb5DH

如果那不起作用:

body {

  background-color: #ffffff;

  font-family: candara, monospace;

  text-align: center;

  font-weight: bold;

  margin-top: 5px;

  text-transform: uppercase;

  height: 600px;

  width: 1000px;

  color: #1b1186;

}


#DELETE {

  background-color: #1b1186;

  width: 250px;

  color: white;

  margin-top: 50px;

}


#DELETE:hover {

  background-color: #ff4625;

  cursor: pointer;

}


button {

  background-color: #1b1186;

  border-radius: 0px;

  border: 0px #cccccc;

  font-family: candara, monospace;

  font-weight: bold;

  margin-top: 15px;

  color: #ffffff;

  text-align: center;

  font-size: 18px;

  padding: 10px;

  width: 200px;

  transition: all 1s;

  cursor: pointer;

  text-transform: uppercase;

  display: inline-block;

  text-decoration: none;

}


button:hover {

  background-color: #fff06d;

  color: black;

  padding-right: 25px;

  padding-left: 25px;

}


table {

  border: 5px, #1b1186

}


ABOUTYOU
浏览 122回答 4
4回答

函数式编程

首先,如果你想显示动态内容,你必须使用数据库,没有别的办法。其次,如果你想让你的内容实时变化,你必须使用 firebase、websocket 或任何其他技术

繁星coding

在此示例中,我使用的是本地存储,并且我创建了一些函数以便您可以处理数据。<html><head>&nbsp; <button type="button" onclick="window.location.href='userhome.html';">Home</button>&nbsp; <button type="button" onclick="window.location.href='settings.html';">Settings</button>&nbsp; <button type="button" onclick="window.location.href='userhome.html';">Add Hours</button>&nbsp; <meta charset="utf-8" />&nbsp; <title></title>&nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />&nbsp; <script src="https://code.jquery.com/jquery-3.3.1.js"></script>&nbsp; <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script></head><body>&nbsp; <table id="HOURTABLE" contenteditable="true" class="display" style="width:100%">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Session</th>&nbsp; &nbsp; &nbsp; &nbsp; <th># Hours</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Organization</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Description</th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody class="body-container">&nbsp; &nbsp; </tbody>&nbsp; &nbsp; <tfoot>&nbsp; &nbsp; </tfoot>&nbsp; &nbsp; <br>&nbsp; &nbsp; <button ondblclick="deleteRowSelected()">Delete Row</button>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; function getData() {&nbsp; &nbsp; &nbsp; &nbsp; let local = localStorage.getItem('data');&nbsp; &nbsp; &nbsp; &nbsp; if (local == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local = setData();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return JSON.parse(local);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; function setData(data = null) {&nbsp; &nbsp; &nbsp; &nbsp; if (data == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hours: 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: '4/5/2020',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; organization: 'Tutoring',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: 'It was fun'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hours: 67,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: '4/8/2020',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; organization: 'Tutoring',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: 'It was interesting'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; const textData = JSON.stringify(data);&nbsp; &nbsp; &nbsp; &nbsp; localStorage.removeItem('data');&nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem('data', textData);&nbsp; &nbsp; &nbsp; &nbsp; return textData;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; function generateRow(row) {&nbsp; &nbsp; &nbsp; &nbsp; return `&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr data-session="${row.session}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>${row.session}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>${row.hours}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>${row.date}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>${row.organization}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>${row.description}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>`;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; function deleteRow(session) {&nbsp; &nbsp; &nbsp; &nbsp; const data = getData();&nbsp; &nbsp; &nbsp; &nbsp; let newArray = [];&nbsp; &nbsp; &nbsp; &nbsp; data.forEach(element => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (element.session !== session) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newArray.push(element);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; console.log(newArray);&nbsp; &nbsp; &nbsp; &nbsp; setData(newArray);&nbsp; &nbsp; &nbsp; &nbsp; load();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; function load() {&nbsp; &nbsp; &nbsp; &nbsp; const data = getData();&nbsp; &nbsp; &nbsp; &nbsp; const container = $('.body-container');&nbsp; &nbsp; &nbsp; &nbsp; container.html('');&nbsp; &nbsp; &nbsp; &nbsp; if (container.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.forEach(row => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.append(generateRow(row));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container.appent('<tr>empty</tr>');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; var x = document.getElementById("HOURTABLE").rows.length;&nbsp; &nbsp; &nbsp; function deleteRowSelected() {&nbsp; &nbsp; &nbsp; &nbsp; const row = $('.body-container').find('tr.selected');&nbsp; &nbsp; &nbsp; &nbsp; if (row.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('you must select a row');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteRow(row.data('session'));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; $(document).ready(function () {&nbsp; &nbsp; &nbsp; &nbsp; var table = $('#HOURTABLE').DataTable();&nbsp; &nbsp; &nbsp; &nbsp; $('#HOURTABLE tbody').on('click', 'tr', function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($(this).hasClass('selected')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).removeClass('selected');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.$('tr.selected').removeClass('selected');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).addClass('selected');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; load();&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; </table></body></html>

守着一只汪

以下示例假定您正在使用 PHP,并且delsessions.php在您的 Web 服务器上设置了一个名为 的 PHP 脚本。此脚本将通过 HTTP POST 接受 ID 数组。然后它将更新 SQL 数据库并从与传递给它的会话 ID 关联的表中删除行。这还假设有脚本或 API 从同一个数据库表中提供表数据。$(function() {&nbsp; var table = $('#HOURTABLE').DataTable();&nbsp; function href(el) {&nbsp; &nbsp; window.location.href = $(el).data("href");&nbsp; }&nbsp; function delRows() {&nbsp; &nbsp; var sessions = [];&nbsp; &nbsp; $(".selected").each(function(i, el) {&nbsp; &nbsp; &nbsp; sessions.push($(el).children().eq(0).text());&nbsp; &nbsp; })&nbsp; &nbsp; table.rows(".selected").remove().draw();&nbsp; &nbsp; console.log("Delete Sessions", sessions);&nbsp; &nbsp; $.post("delsessions.php", {&nbsp; &nbsp; &nbsp; ids: sessions&nbsp; &nbsp; });&nbsp; }&nbsp; $(".btn[data-href]").click(function(e) {&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; href(this);&nbsp; });&nbsp; $(".delete-row").click(delRows);&nbsp; $('#HOURTABLE tbody').on('click', 'tr', function() {&nbsp; &nbsp; $(this).toggleClass("selected");&nbsp; });});body {&nbsp; background-color: #ffffff;&nbsp; font-family: candara, monospace;&nbsp; text-align: center;&nbsp; font-weight: bold;&nbsp; margin-top: 5px;&nbsp; text-transform: uppercase;&nbsp; height: 600px;&nbsp; width: 1000px;&nbsp; color: #1b1186;}#DELETE {&nbsp; background-color: #1b1186;&nbsp; width: 250px;&nbsp; color: white;&nbsp; margin-top: 50px;}#DELETE:hover {&nbsp; background-color: #ff4625;&nbsp; cursor: pointer;}button {&nbsp; background-color: #1b1186;&nbsp; border-radius: 0px;&nbsp; border: 0px #cccccc;&nbsp; font-family: candara, monospace;&nbsp; font-weight: bold;&nbsp; margin-top: 15px;&nbsp; color: #ffffff;&nbsp; text-align: center;&nbsp; font-size: 18px;&nbsp; padding: 10px;&nbsp; width: 200px;&nbsp; transition: all 1s;&nbsp; cursor: pointer;&nbsp; text-transform: uppercase;&nbsp; display: inline-block;&nbsp; text-decoration: none;}button:hover {&nbsp; background-color: #fff06d;&nbsp; color: black;&nbsp; padding-right: 25px;&nbsp; padding-left: 25px;}table {&nbsp; border: 5px, #1b1186}<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" /><script src="https://code.jquery.com/jquery-3.3.1.js"></script><script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script><button class="home btn" data-href="userhome.html">Home</button><button class="settings btn" data-href="settings.html">Settings</button><button class="add-hours btn" data-href="userhome.html">Add Hours</button><button class="delete-row btn">Delete Row</button><table id="HOURTABLE" contenteditable="true" class="display" style="width:100%">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>Session</th>&nbsp; &nbsp; &nbsp; <th># Hours</th>&nbsp; &nbsp; &nbsp; <th>Date</th>&nbsp; &nbsp; &nbsp; <th>Organization</th>&nbsp; &nbsp; &nbsp; <th>Description</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>1</th>&nbsp; &nbsp; &nbsp; <th>4</th>&nbsp; &nbsp; &nbsp; <th>4/5/2020</th>&nbsp; &nbsp; &nbsp; <th>Tutoring</th>&nbsp; &nbsp; &nbsp; <th>It was fun</th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>2</th>&nbsp; &nbsp; &nbsp; <th>67</th>&nbsp; &nbsp; &nbsp; <th>4/8/2020</th>&nbsp; &nbsp; &nbsp; <th>Tutoring</th>&nbsp; &nbsp; &nbsp; <th>It was interesting</th>&nbsp; &nbsp; </tr>&nbsp; </tbody>&nbsp; <tfoot>&nbsp; </tfoot></table>当用户选择时,通过click各种行并单击“删除行”按钮,数据表将被更新,删除那些行,并且这些行的 ID 将发布到 PHP。然后脚本将从数据库中删除相关行。当用户返回站点或重新加载站点时,数据库表将不再包含这些行,并且在构建 HTML 表时也不会显示它们。

慕婉清6462132

如果没有像 PHP、node.js、firebase 这样的后端,你就无法做到这一点……您可以使用 localStorage 进行黑客攻击,但仅当用户不删除浏览器数据时,更改才会保留。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript