所以我有代码:
$(".redd").click(function() {
fillcolor = $(".redd").css("background-color");
});
$(".pink").click(function() {
fillcolor = $(".pink").css("background-color");
});
$(".purple").click(function() {
fillcolor = $(".purple").css("background-color");
});
$(".indigo").click(function() {
fillcolor = $(".indigo").css("background-color");
});
$(".cyan").click(function() {
fillcolor = $(".cyan").css("background-color");
});
$(".blue").click(function() {
fillcolor = $(".blue").css("background-color");
});
$(".yellow").click(function() {
fillcolor = $(".yellow").css("background-color");
});
$(".green").click(function() {
fillcolor = $(".green").css("background-color");
});
$(".orange").click(function() {
fillcolor = $(".orange").css("background-color");
});
$(".black").click(function() {
fillcolor = $(".black").css("background-color");
});
这段代码有什么作用? 例如$(" .redd")是按钮,点击它会改变变量' fillcolor'到按钮的背景颜色。 如何更聪明地编写这段代码?
答案 0 :(得分:3)
给他们一个公共类并使用$(this)来获取点击的那个
USE [TestDB]
GO
/****** Object: StoredProcedure [dbo].[GetUserBasedOnHobby] Script Date: 12/9/2017 3:14:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetUserBasedOnHobby]
@hobbyName varchar(50)
AS
SELECT * from dbo.UserTable where dbo.UserTable.Hobby = @hobbyName;
RETURN 0
GO

$(".color").click(function() {
fillcolor = $(this).css("background-color");
console.log(fillcolor);
});

.green {
background-color: green
}
.blue {
background-color: blue
}

答案 1 :(得分:1)
这是我的方法,随时添加建议!
var arry = [".redd", ".pink", ...]
for (var i = 0; i < arry.length; i++)
{
$(arry[i]).click(function() {
fillcolor = $(".purple").css("background-color");
})
}