如何从div中获得一个数字,然后相乘



我的结果如下显示50个条目中的5到40个我需要得到的总条目忽略"显示5到40 "保持"50"这个数字;将其插入到输入总数中,然后乘以10

<div class="info" id="info">Showing 30 to 40 of 50 entries</div>
<input type="text" id="total" name="total">
<input type="text" id="total" name="x10">

这个答案是用strong假设文本总是显示"显示50个条目中的5到40个";或者类似的文本,你还需要改变其中一个框的id,因为所有的id都应该是唯一的,以便在js代码中可用

// use js to get the value
const info = document.getElementById("info").innerHTML
// get an array of text seperated by space
const texts = info.split(" ")
// get the 2nd last character
const number = parseInt(texts[texts.length - 2])
// put those values into the input boxes
document.getElementById("total").value = number
document.getElementById("totalTimes10").value = number*10
<div class="info" id="info">Showing 30 to 40 of 50 entries</div>
<input type="text" id="total" name="total">
<input type="text" id="totalTimes10" name="x10">

利用节点的Node.textContent属性得到div的text content,再用String.split()方法提取总条目

const text = document.querySelector("#info").textContent.split(" ");
const entries = text[text.length - 2];
document.querySelector("#total").value = entries;
document.querySelector("#totalTimes10").value = 10 * entries;
<div class="info" id="info">Showing 30 to 40 of 50 entries</div>
<input type="text" id="total" name="total">
<input type="text" id="totalTimes10" name="x10">

最新更新