gpt4 book ai didi

javascript - 从触发事件的元素中返回属性 id

转载 作者:可可西里 更新时间:2023-11-01 02:29:24 25 4
gpt4 key购买 nike

我有一个元素,这个元素通过以下代码调用函数 calcTotal:

$('.pause').change(function(e) {
window.alert("pause changed");
calcTotal(e);

calcTotal(e)的代码如下:

function calcTotal(event)
{ alert('calcTotal called');
var myId = event.currentTarget.attr('id');

myId = myId.replace(/[^0-9]/g, '');

var timeRegex = /^[0-9]{1,2}:[0-9]{2}$/;

if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) && $('#pause'+myId).val().match(timeRegex))
{
var minutes = 0;

var n = $('#end'+myId).val().split(':');
minutes = parseInt(n[0])*60 + parseInt(n[1]);

var n = $('#start'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);

var n = $('#pause'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);

var hours = Math.floor(minutes/60);
minutes = minutes % 60;
alert(hours + ':' + minutes);
$('#total' + myId).val(hours + ':' + minutes);
}
else
{
$('#total' + myId).val('00:00');
}

}

它没有像我预期的那样工作,当我用 firebug 调试时它说如下:

TypeError: event.currentTarget.attr is not a function
var myId = event.currentTarget.attr('id');

我想将元素的 ID 存储在 myId 中。我该怎么做?

最佳答案

event.currentTarget 不是 jQuery 对象,它是 DOM 节点。

var myIf = event.currentTarget.id;

关于javascript - 从触发事件的元素中返回属性 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com