单个JavaScript单击事件中的多个ID [英] Multiple id's in a single JavaScript click event
问题描述
在JavaScript中,我使用click事件来更改图表数据。以下是点击事件的方法。
In JavaScript I am using click event to change chart data. Below is a method for click event.
$('#pro1').click(function () {
chart.series[0].update({
data: pro1
});
});
$('#pro2').click(function () {
chart.series[0].update({
data: pro2
});
});
$('#pro3').click(function () {
chart.series[0].update({
data: pro3
});
});
我需要在一个事件中缩小这三个点击事件,这意味着我想写一个点击事件处理id。有点像下面的代码。
I need to minify these three click events in one event, means I want to write one click event which handle the id's. some thing like below code.
$('#pro'+i).click(function () {
chart.series[0].update({
data: pro+i
});
});
我不知道该怎么做。上面的代码不正确,只是我对JavaScript缺乏了解。
I don't know how to do it exactly. The above code is not correct, it is just my lack of knowledge of JavaScript.
推荐答案
我建议创建一个对象并选择使用类的元素,单击元素的 id
检索辅助对象的相应属性的值:
I would suggest creating an object and selecting the elements using classes, id
of the clicked element retrieves value of the corresponding property of the helper object:
var pros = {
pro1: '...',
pro2: '...'
};
$('.pros').click(function () {
chart.series[0].update({
data: pros[this.id]
});
});
这篇关于单个JavaScript单击事件中的多个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!