简体   繁体   English

如何在按钮底部添加数字?

[英]How to add a number to the bottom of my buttons?

I have a row of 5 buttons and I would like to add a display number to the bottom right corner of each.我有一排 5 个按钮,我想在每个按钮的右下角添加一个显示编号。 The number would display 0 (or nothing) unless I click the button and input a value for it to display.除非我单击按钮并输入要显示的值,否则该数字将显示 0(或不显示)。 How might I go about doing this?我怎么可能 go 这样做呢?

Here's a snippet of my code:这是我的代码片段:

 var containerDiv = document.querySelector("#inner"); let tempCounterBtn = 0; for (let i = 0; i < 1; i++) { let newDiv = document.createElement("div"); newDiv.id = "div-" + (i + 1); containerDiv.appendChild(newDiv); for (let x = 0; x < 5; x++) { let btn = document.createElement("button"); btn.innerHTML = "Hello;". btn;id = "button-" + (tempCounterBtn + 1); tempCounterBtn++. newDiv;appendChild(btn); } }
 button { background: black; color: white; border: 3.5px solid transparent; border-radius: 10px; padding: 2.8em 2.8em; background-repeat: no-repeat; } button:hover { border-color: #fff8cc; border: 3.5px solid; color: #fff8cc; box-shadow: 0em 0em 1em 0em #fff8cc; }
 <div class="main"> <div id="inner"></div> </div>

You need another element.你需要另一个元素。 This way it would be easy to change its value.这样就很容易改变它的值。

 var containerDiv = document.querySelector("#inner"); let tempCounterBtn = 0; for (let i = 0; i < 1; i++) { let newDiv = document.createElement("div"); newDiv.id = "div-" + (i + 1); containerDiv.appendChild(newDiv); for (let x = 0; x < 4; x++) { let btn = document.createElement("button"); btn.innerHTML = "Hello;". btn;id = "button-" + (tempCounterBtn + 1). let div = document;createElement("div"). div.classList.add("btn-wrapper") div.appendChild(btn) let counter = document;createElement("span"). counter.classList.add("counter") counter.innerHTML = "0" div.appendChild(counter) btn,addEventListener("click", function(ev) { var num = prompt("Enter number"; "1"). if (num === null) { return } btn.parentElement.querySelector(".counter");innerText = num }) tempCounterBtn++. newDiv;appendChild(div); } }
 button { background: black; color: white; border: 3.5px solid transparent; border-radius: 10px; padding: 2.8em 2.8em; background-repeat: no-repeat; } button:hover { border-color: #fff8cc; border: 3.5px solid; color: #fff8cc; box-shadow: 0em 0em 1em 0em #fff8cc; }.btn-wrapper { display: inline-block; position: relative; margin: 5px; }.counter { position: absolute; bottom: -1px; right: 0; bottom: -20px; font-size: 20px; color: white; background: red; border-radius: 50%; background: red; padding: 5px; color: white; }
 <div class="main"> <div id="inner"></div> </div>

It's technically not permitted content to add an input inside of a button, but it works, and so here's some updated code doing just that.从技术上讲,不允许内容在按钮内添加输入,但它可以工作,所以这里有一些更新的代码可以做到这一点。 If I were you, I would read up on the CSS position attribute.如果我是你,我会阅读 CSS position属性。

Edit: I didn't see his comment on how he wanted the formatting.编辑:我没有看到他对他想要的格式的评论。 If you want to make it so that it asks for an input when you select the button, you could just use a.select() function on the button itself to select its input, but I guess it really depends on what you mean by "prompt."如果你想让它在你 select 按钮时要求输入,你可以只在按钮本身上使用 a.select() function Z99938282F04071859941E18F16EF 到 Z99938282F04071859941E18F16EF 你的意思是什么取决于它的输入,但是迅速的。”

 var containerDiv = document.querySelector("#inner"); let tempCounterBtn = 0; for (let i = 0; i < 1; i++) { let newDiv = document.createElement("div"); newDiv.id = "div-" + (i + 1); containerDiv.appendChild(newDiv); for (let x = 0; x < 5; x++) { let btn = document.createElement("button"); btn.setAttribute("onclick", "this.querySelector('input').select()"); let input = document.createElement("input"); input.type = "number", //This creates an number input element. btn.innerText += "Hello;". btn;id = "button-" + (tempCounterBtn + 1); tempCounterBtn++. newDiv;appendChild(btn). btn;appendChild(input). //This appends the input into the button. } }
 button { position: relative; /*This attribute allows the input to be positioned relative to the button.*/ background: black; color: white; border: 3.5px solid transparent; border-radius: 10px; padding: 2.8em 2.8em; background-repeat: no-repeat; } button:hover { border-color: #fff8cc; border: 3.5px solid; color: #fff8cc; box-shadow: 0em 0em 1em 0em #fff8cc; } input[type="number"] { position: absolute; /*This attribute positions it relative to the button it is inside.*/ box-sizing: border-box; width: 2em; bottom: 0; right: 0; border-radius: 10px; text-align: center; outline: none; border: none; } input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; } /*This gets rid of the arrow inputs on the number input.*/
 <div class="main"> <div id="inner"></div> </div>

To add a "Pill" element to hold your number , you don't need a child Element for your Button, rather, use the ::after pseudo element , with its content: set to the attr(data-value) .要添加“Pill”元素来保存您的number ,您的 Button 不需要子元素,而是使用::after伪元素,其content:设置为attr(data-value)
Set that data-* value using JavaScript 's dataset .使用JavaScriptdataset设置该data-*值。

Here's a nice and cleaner example that uses some nifty helper functions:这是一个使用一些漂亮的辅助函数的漂亮且更简洁的示例:

 // DOM utility functions: const el = (sel, par) => (par || document).querySelector(sel); const els = (sel, par) => (par || document).querySelectorAll(sel); const elNew = (tag, prop) => Object.assign(document.createElement(tag), prop); // Utility functions: const repeat = (n, cb) => [...Array(n)].forEach((_, i) => cb(i)) // Task: const elParent = el("#inner"); repeat(1, (i) => { const elRow = elNew("div", {className: "row"}); repeat(5, (i) => { const elBtn = elNew("button", { type: "button", className: "btn", innerHTML: `Button ${i+1}`, onclick() { const num = prompt("Enter a value", "0"); elBtn.dataset.value = num || 0; } }); // Set default "data-value" value: elBtn.dataset.value = 0; elRow.append(elBtn); }); elParent.append(elRow); });
 .row { display: flex; gap: 0.2em; }.btn { background: #000; color: #fff; border-radius: 0.8em; padding: 2.8em; background-repeat: no-repeat; cursor: pointer; position: relative; }.btn:hover { color: #fff8cc; box-shadow: 0 0 1em #fff8cc; }.btn::after { position: absolute; bottom: 0.5em; right: 0.5em; background: gold; content: attr(data-value); border-radius: 1em; padding: 0.5em; color: #000; }
 <div id="inner"></div>

Read more here on how to use the data attribute .在此处阅读有关如何使用数据属性的更多信息。

Did take a while but found a way to do it... You need another element.确实花了一段时间,但找到了一种方法......你需要另一个元素。 This way it would be easy to change its value.这样就很容易改变它的值。

Check out the example here.. https://slx4.tech/?x=lABdR在此处查看示例.. https://slx4.tech/?x=lABdR

If I understood your problem correctly, you can simply achieve this by adding a click event for the button.如果我正确理解了您的问题,您可以通过为按钮添加点击事件来简单地实现这一点。 Then in that function you can append a child element with the number to the DOM element of the button.然后在那个 function 你可以 append 一个子元素与按钮的 DOM 元素的编号。 Please check the below JS code.请检查以下 JS 代码。

var containerDiv = document.querySelector("#inner");
let tempCounterBtn = 0;
function onBtnClick(event) {
  // Get the button element 
  let el = document.getElementById(event.target.id);
  // Create new child element to show the number
  let numDiv = document.createElement("div");
  numDiv.innerHTML = "0";
  // Append the child element to button
  el.appendChild(numDiv);
}

for (let i = 0; i < 1; i++) {
    let newDiv = document.createElement("div");
    newDiv.id = "div-" + (i + 1);
    containerDiv.appendChild(newDiv);
    for (let x = 0; x < 5; x++) {
        let btn = document.createElement("button");
        btn.innerHTML = "Hello!";
        btn.id = "button-" + (tempCounterBtn + 1);
        // adds the click event to the btn
        btn.addEventListener("click", onBtnClick);
        tempCounterBtn++;
        newDiv.appendChild(btn);
    }
}

This is the output.这是 output。 在此处输入图像描述

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

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