简体   繁体   English

样式化/执行动态创建的按钮

[英]Styling/executing dynamically created buttons

I'm working on a library project, where a user can 'add' a book to the library(html table) with the title of the book, the author and the amount of pages.我正在做一个图书馆项目,用户可以在图书馆(html 表)中“添加”一本书,其中包含书名、作者和页数。 I've also used js to add 2 buttons into the table to mark whether the book has been read, and an option to remove the book from the library.我还使用 js 在表格中添加了 2 个按钮来标记该书是否已阅读,以及从图书馆中删除该书的选项。

The buttons are appended when the user clicks 'Add Book', so I've given each button a unique id with Date.now(), but now I'm not sure how to access the buttons with css.当用户单击“添加图书”时,会附加这些按钮,因此我使用 Date.now() 为每个按钮提供了唯一的 ID,但现在我不确定如何使用 css 访问按钮。

I'm not sure if people share repl's on this but I can if that would make it easier, and I'm fairly new to js so any help would be greatly appreciated!我不确定人们是否会在这方面分享 repl,但如果这会让事情变得更容易,我可以,而且我对 js 还很陌生,所以任何帮助都将不胜感激!

function addBook() {
  document.querySelector('#addbook').addEventListener("click", () => {

    // First, hijack our original buttons...
    const readButton = document.querySelector('#readed').cloneNode(true);
    const removeButton = document.querySelector('#remove').cloneNode(true);

    //  then create a fake id for this book. This 
    //  particular value is simply the ms since
    //  01/01/1970 00:00:00.000

    const bookId = Date.now();

    // And lastly, we can use that value, which we've 
    // tied to the book itself, to update these buttons

    readButton.id = `${readButton.id}_${bookId}`;
    removeButton.id = `${removeButton.id}_${bookId}`;

I'm not entirely sure what your question is, but if it is regarding how to change the styling of the dynamically created buttons, here is a couple of ways:我不完全确定您的问题是什么,但如果是关于如何更改动态创建的按钮的样式,这里有几种方法:

Please read this for more details: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style请阅读此内容以获取更多详细信息: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

Using the.style property on a node you can add inline styles, like changing a color or background.使用节点上的 .style 属性,您可以添加内联 styles,例如更改颜色或背景。 You can simply do:你可以简单地做:

readButton.style.cssText = "color: blue; border: 1px solid black";

or if you want to change just one attribute without touching the rest:或者如果您只想更改一个属性而不接触 rest:

readButton.style.color = "blue";

Did that help?那有帮助吗? If not, let me know and I'll update the answer.如果没有,请告诉我,我会更新答案。

If you want consistent styling try using a class:如果您想要一致的样式,请尝试使用 class:

function addBook() {
document.querySelector('#addbook').addEventListener("click", () => {

// First, hijack our original buttons...
const readButton = document.querySelector('#readed').cloneNode(true);
const removeButton = document.querySelector('#remove').cloneNode(true);

// add classes 
readButton.classList.add('readBtn');
removeButton.classList.add('removeBtn');

//  then create a fake id for this book. This 
//  particular value is simply the ms since
//  01/01/1970 00:00:00.000

const bookId = Date.now();

// And lastly, we can use that value, which we've 
// tied to the book itself, to update these buttons

readButton.id = `${readButton.id}_${bookId}`;
removeButton.id = `${removeButton.id}_${bookId}`;

And style the css accordingly:并相应地设置 css 的样式:

.readBtn {
  background-color: blue;
  etc...
 }

.removeBtn {
  background-color: red;
  etc...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM