亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使 HTML 網站具有交互性,以便當用戶單擊刪除按鈕時它會為所有用戶更改

如何使 HTML 網站具有交互性,以便當用戶單擊刪除按鈕時它會為所有用戶更改

ABOUTYOU 2022-12-22 12:18:13
我用 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}
查看完整描述

4 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

首先,如果你想顯示動態內容,你必須使用數據庫,沒有別的辦法。其次,如果你想讓你的內容實時變化,你必須使用 firebase、websocket 或任何其他技術



查看完整回答
反對 回復 2022-12-22
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

在此示例中,我使用的是本地存儲,并且我創建了一些函數以便您可以處理數據。


<html>


<head>

  <button type="button" onclick="window.location.href='userhome.html';">Home</button>

  <button type="button" onclick="window.location.href='settings.html';">Settings</button>

  <button type="button" onclick="window.location.href='userhome.html';">Add Hours</button>

  <meta charset="utf-8" />

  <title></title>

  <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>


</head>


<body>

  <table id="HOURTABLE" contenteditable="true" class="display" style="width:100%">

    <thead>

      <tr>

        <th>Session</th>

        <th># Hours</th>

        <th>Date</th>

        <th>Organization</th>

        <th>Description</th>

      </tr>

    </thead>

    <tbody class="body-container">



    </tbody>

    <tfoot>

    </tfoot>

    <br>

    <button ondblclick="deleteRowSelected()">Delete Row</button>


    <script>


      function getData() {


        let local = localStorage.getItem('data');


        if (local == null) {


          local = setData();


        }


        return JSON.parse(local);


      }


      function setData(data = null) {


        if (data == null) {

          data =

            [

              {

                session: 1,

                hours: 4,

                date: '4/5/2020',

                organization: 'Tutoring',

                description: 'It was fun'

              },

              {

                session: 2,

                hours: 67,

                date: '4/8/2020',

                organization: 'Tutoring',

                description: 'It was interesting'

              }

            ];


        }


        const textData = JSON.stringify(data);

        localStorage.removeItem('data');

        localStorage.setItem('data', textData);

        return textData;



      }


      function generateRow(row) {


        return `

            <tr data-session="${row.session}">

              <th>${row.session}</th>

              <th>${row.hours}</th>

              <th>${row.date}</th>

              <th>${row.organization}</th>

              <th>${row.description}</th>

            </tr>`;


      }


      function deleteRow(session) {


        const data = getData();

        let newArray = [];


        data.forEach(element => {



          if (element.session !== session) {


            newArray.push(element);

          }

        })

        console.log(newArray);

        setData(newArray);

        load();


      }

      function load() {


        const data = getData();

        const container = $('.body-container');

        container.html('');

        if (container.length > 0) {

          data.forEach(row => {


            container.append(generateRow(row));


          })


        } else {

          container.appent('<tr>empty</tr>');

        }


      }

      var x = document.getElementById("HOURTABLE").rows.length;


      function deleteRowSelected() {


        const row = $('.body-container').find('tr.selected');

        if (row.length == 0) {

          alert('you must select a row');

        } else {

          row.remove();

          deleteRow(row.data('session'));

        }


      }



      $(document).ready(function () {

        var table = $('#HOURTABLE').DataTable();

        $('#HOURTABLE tbody').on('click', 'tr', function () {

          if ($(this).hasClass('selected')) {

            $(this).removeClass('selected');

          }

          else {

            table.$('tr.selected').removeClass('selected');

            $(this).addClass('selected');

          }

        });


        load();

      });


    </script>


  </table>



</body>


</html>



查看完整回答
反對 回復 2022-12-22
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

以下示例假定您正在使用 PHP,并且delsessions.php在您的 Web 服務器上設置了一個名為 的 PHP 腳本。此腳本將通過 HTTP POST 接受 ID 數組。然后它將更新 SQL 數據庫并從與傳遞給它的會話 ID 關聯的表中刪除行。


這還假設有腳本或 API 從同一個數據庫表中提供表數據。


$(function() {

  var table = $('#HOURTABLE').DataTable();


  function href(el) {

    window.location.href = $(el).data("href");

  }


  function delRows() {

    var sessions = [];

    $(".selected").each(function(i, el) {

      sessions.push($(el).children().eq(0).text());

    })

    table.rows(".selected").remove().draw();

    console.log("Delete Sessions", sessions);

    $.post("delsessions.php", {

      ids: sessions

    });

  }


  $(".btn[data-href]").click(function(e) {

    e.preventDefault();

    href(this);

  });


  $(".delete-row").click(delRows);


  $('#HOURTABLE tbody').on('click', 'tr', function() {

    $(this).toggleClass("selected");

  });

});

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

}

<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%">

  <thead>

    <tr>

      <th>Session</th>

      <th># Hours</th>

      <th>Date</th>

      <th>Organization</th>

      <th>Description</th>

    </tr>

  </thead>

  <tbody>

    <tr>

      <th>1</th>

      <th>4</th>

      <th>4/5/2020</th>

      <th>Tutoring</th>

      <th>It was fun</th>

    </tr>

    <tr>

      <th>2</th>

      <th>67</th>

      <th>4/8/2020</th>

      <th>Tutoring</th>

      <th>It was interesting</th>

    </tr>

  </tbody>

  <tfoot>

  </tfoot>

</table>


當用戶選擇時,通過click各種行并單擊“刪除行”按鈕,數據表將被更新,刪除那些行,并且這些行的 ID 將發布到 PHP。然后腳本將從數據庫中刪除相關行。當用戶返回站點或重新加載站點時,數據庫表將不再包含這些行,并且在構建 HTML 表時也不會顯示它們。


查看完整回答
反對 回復 2022-12-22
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

如果沒有像 PHP、node.js、firebase 這樣的后端,你就無法做到這一點……

您可以使用 localStorage 進行黑客攻擊,但僅當用戶不刪除瀏覽器數據時,更改才會保留。


查看完整回答
反對 回復 2022-12-22
  • 4 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號