从触发事件的元素返回属性id [英] Return the attribute id from the element that triggerd the event
本文介绍了从触发事件的元素返回属性id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个元素,这个元素通过以下代码调用函数calcTotal:
I have an element, this element calls the function calcTotal via the following code:
$('.pause').change(function(e) {
window.alert("pause changed");
calcTotal(e);
calcTotal(e)的代码如下:
The code of calcTotal(e) is as follows:
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进行调试时,它说如下:
It doesn't work as I had exceptected and when I debug with firebug it says the following:
TypeError: event.currentTarget.attr is not a function
var myId = event.currentTarget.attr('id');
我想在myId中存储元素的id。
我该怎么做?
I would like to store the id of the element in myId. How would I do this?
推荐答案
event.currentTarget
不是jQuery对象,它是一个DOM节点。
event.currentTarget
is not a jQuery object, it's a DOM node.
var myIf = event.currentTarget.id;
这篇关于从触发事件的元素返回属性id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文