I have 3 img
elements with ID's.
(我有3个具有ID的img
元素。)
When I click the element, I change the img src
like this(当我单击元素时,我会像这样更改img src
)
$("#employee").on("click", function(){
var x = $(this);
var y = $(this).attr("src");
if($(this).attr("src") == "images/employee.svg")
{
$(this).attr("src", "images/employee_selected.svg");
}
else
{
$(this).attr("src", "images/employee.svg");
}
});
$("#team").on("click", function(){
var x = $(this);
var y = $(this).attr("src");
if($(this).attr("src") == "images/team.svg")
{
$(this).attr("src", "images/team_selected.svg");
}
else
{
$(this).attr("src", "images/team.svg");
}
});
$("#product").on("click", function(){
var x = $(this);
var y = $(this).attr("src");
if($(this).attr("src") == "images/product.svg")
{
$(this).attr("src", "images/product_selected.svg");
}
else
{
$(this).attr("src", "images/product.svg");
}
});
This code seems to be awfully repetitive and I was wondering if there was a way to do this in a single .on click function
using something like a switch statement.
(这段代码似乎非常重复,我想知道是否有一种方法可以在单个.on click function
使用switch语句之类的方法。)
How could I simplify this into a single statement?
(如何将其简化为一个语句?)
ask by JuiceBox translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…