微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 JavaScript 中单击编辑按钮后如何编辑 td?

如何解决在 JavaScript 中单击编辑按钮后如何编辑 td?

我正在开发一个 oop 项目,我想用它来为自己使用并添加到我的投资组合中。我也将它用作学习项目,因为我是 oop 的新手。 在这个项目中,我希望能够编辑提交到底部表格的添加的 td 字段。 当我尝试创建输入元素,然后将输入附加到提交的项目时,它只会在 tds 旁边创建输入框。

(更新我现在可以编辑第一个 td 项目,我现在正在点击 Enter 或向应用添加确定按钮后更新编辑。)

这是我的 html css 和 js:

// this is the es5 way to do it with constructors and prototypes
// keyword constructor

function Keyword(sitename,keyword,keywordpost) {
  this.sitename = sitename;
  this.keyword = keyword;
  this.keywordpost = keywordpost;
}



// UI Constructor is the things assosiated to the ui different prototypes and methods
function UI() {

}
// this is the UI prototype for the above UI constructor
UI.prototype.addKeywordToList = function(keywordNames) {
  const list = document.getElementById('keyword-list');
  // create tr element
  const row = document.createElement('tr');
  // insert cols
  row.innerHTML = `
    <td>${keywordNames.sitename}</td>
    <td>${keywordNames.keyword}</td>
    <td>${keywordNames.keywordpost}</td>
    <td><a href='#' class="delete">X</a></td>
    <td><a href='#' class="edit">edit</a></td>
    `;
  console.log(row);
  console.log(list);



  list.appendChild(row);
}
// start here on monday or tuesday ------------------------------------------------------
// show alert
UI.prototype.showAlert = function(message,className) {
  //create div
  const div = document.createElement('div');
  // add class
  div.className = `alert ${className}`;
  // add text
  div.appendChild(document.createTextNode(message));
  // Get parent
  const container = document.querySelector('.input-container');
  const form = document.querySelector('#keyword-form');
  //insert alert Box/message above the form
  container.insertBefore(div,form);
  // Timeout after 3 secs
  setTimeout(function() {
    document.querySelector('.alert').remove();
  },3000);
}


// clear fields of there input
UI.prototype.clearFields = function() {
  document.getElementById('website-name').value = '';
  document.getElementById('keyword-name').value = '';
  document.getElementById('keyword-post').value = '';
}

//prototype to delete item
UI.prototype.deleteBook = function(target) {
  if (target.className === 'delete') {
    target.parentElement.parentElement.remove();

  }
}

//prototype to edit book -------- working on this here updated Now edits element but just need save the update
UI.prototype.editBook = function(target) {


  if(target.className === 'edit'){
    const firstItem = document.querySelector('td');
    const input = document.createElement('input');
    firstItem.innerHTML = '';
    input.type = 'text';
    firstItem.appendChild(input);


    console.log(firstItem);



  }

}


// Event Listeners

document.getElementById('keyword-form').addEventListener('submit',function(e) {
  // get form values
  const siteName = document.getElementById('website-name').value
  const keywordName = document.getElementById('keyword-name').value
  const keywordPost = document.getElementById('keyword-post').value


  //instantiate a book creating a new one based of the Keyword object constructor ---------------------------
  const keywordNames = new Keyword(siteName,keywordName,keywordPost);


  //instantiate UI object ----------------------------------------------------

  const ui = new UI();

  //create validation to stpp crosses being submited on a line when there is nothing to submit
  if (siteName === '' || keywordName === '' || keywordPost === '') {
    ui.showAlert('please fill in all fields','error');
  } else {
    ui.addKeywordToList(keywordNames);
    ui.clearFields();
    ui.showAlert('Your item has been added','success');
  }


  console.log('test');

  e.preventDefault();
});



//event listener for delete
document.getElementById('keyword-list').addEventListener('click',function(e) {
  console.log(123);

  //instatiate ui again beceuse we outside of above function
  const ui = new UI();


  ui.deleteBook(e.target);
  ui.showAlert('Your item has been removed','success');



  e.preventDefault();
});



//event listener for editing the table items --------- working on this here
document.getElementById('keyword-list').addEventListener('click',function(e) {


  const ui = new UI();

  ui.editBook(e.target);

  e.preventDefault();
});
* {
  margin: 0;
  Box-sizing: border-Box;
}

.main-container {
  width: 100%;
  height: 100vh;
}

.input-container {
  display: flex;
  height: 50%;
  width: 90%;
  margin: auto;
  flex-direction: column;
  justify-content: space-around;
}

.inputContainer {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  flex-wrap: wrap;
}

#keyword-form {
  display: flex;
  flex-direction: column;
  align-content: space-between;
  justify-content: space-evenly;
  flex-wrap: wrap;
  width: 100%;
  /* padding-top: 6px; */
  height: 90%;
}

.input {
  padding: 10px;
}

.submit-button {
  padding: 10px;
  width: 120px;
}

.output-table {
  width: 100%;
}

tr {
  text-align: center;
}

.success,.error {
  color: white;
  padding: 5px;
  margin: 5px 0 15px 0;
}

.success {
  background: green;
}

.error {
  background: red;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <Meta charset="UTF-8">
  <Meta http-equiv="X-UA-Compatible" content="IE=edge">
  <Meta name="viewport" content="width=device-width,initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>


  <div class="main-container">

    <div class="input-container">
      <h1>SEO Keyword Tracker</h1>
      <form id="keyword-form">
        <div class="inputContainer">
          <label for="Website-Name">Website Name</label>
          <input class="input one" id="website-name" type="text" placeholder="keyword">
        </div>
        <div class="inputContainer">
          <label for="Keyword-Name">Keyword Name</label>
          <input class="input two" id="keyword-name" type="text" placeholder="keyword">
        </div>
        <div class="inputContainer">
          <label for="Keyword-Site-Number">Keyword Post Title</label>
          <input class="input three" id="keyword-post" type="text" placeholder="keyword">
        </div>
        <div class="inputContainer">
          <input class="submit-button" id="Keyword-Site-Name" type="submit" placeholder="keyword">
        </div>
      </form>
    </div>

    <hr>
    <table class="output-table">
      <thead>
        <tr>
          <th>Website Name</th>
          <th>Keyword Name</th>
          <th>Keyword Post Title</th>
          <th></th>
        </tr>
      </thead>
      <tbody id="keyword-list"></tbody>
    </table>


  </div>




  <script src="app.js"></script>
  <script src="appes6.js"></script>
</body>

</html>

好的,所以如果您运行代码段,您将看到我在检查元素时也提到的输入框,在我看来,它们似乎创建了一个新的 tr,然后输入在其中输出。 我不确定我是不是出错了,我想也许我可以按类或 id 获取 td,但它们没有动态创建的类和 id。 我想也许可以获取添加)原型一中使用的 innerHTML 字段并将其添加到(编辑)原型一中。

(更新我现在已经得到它来编辑第一个 td 项目,我现在正在点击 Enter 或向应用添加确定按钮后更新编辑。)

因此,如果有人能提供帮助,我将非常感谢我对这篇文章的更新。

非常感谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。