javascript - 如何从这个类元素中获取数字?

标签 javascript

计时器从 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/

相关文章:

javascript - Angular 不显示 Controller 中传递的 ng 重复的 $scope 数据。也没有错误

javascript - 访问 PDF.js 查看器功能/事件

使用 for 循环的 JavaScript 倒计时

javascript - 在 Aurelia 中动态加载内容后执行函数

javascript - 根据类获取元素的Jquery索引

javascript - 网站代码的基本 Google 登录在 Internet Explorer 11 中不起作用

javascript - 谷歌地图 API v3 : is there any function to check if a coordinate is inside a province or country

javascript - 适用于 Chrome 以外浏览器的桌面通知 API?

Javascript:尝试将项目从一个数组随机移动到另一个数组

javascript - 如何删除字符串周围的特定字符?