计时器从 40 变为 0,脚本应等到 100 毫秒后按下按钮。
如何获得该数字,使其变为 x = 14.22 *1000-100?
我知道我需要删除字母,但是如何删除?
请只使用 javacript,不要使用 jquery!
var evt = document.createEvent("MouseEvents");
var run = document.getElementByClassName("button1");
setInterval(function() {
var time = document.getElementByClassName("Timer h4").innerHTML;
var x = time * 1000 - 100;
setTimeout(function() {
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
run.dispatchEvent(evt);
}, x);
}, 15000);
function x3a() {
document.getElementById("x3").innerHTML = 1;
}
<h4 class="Timer h4">Time expire in 14.22</h4> //how can I read this number 14.22 so I can use it?
<p id="x3"></p>
<button data-g="red" class="button1" onclick="x3a()">OK</button>
最佳答案
选择 h4
元素,并使用正则表达式来匹配数字。
const el = document.querySelector('h4');
const txt = el.innerText.match(/\d+\.\d+/)[0];
console.log(txt);
<h4 class="Timer h4">Time expire in 14.22</h4>
关于javascript - 如何从这个类元素中获取数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449265/