1
我有32個html元素,其ID名稱爲nyg
,nyj
和其他一些。我的目標是將mouseover
和mouseout
事件綁定到每個元素,因此我可以更改用戶懸停的框的color
。這不起作用,我相信有更好的方法來做到這一點,同時也是代碼效率。我正在爲32個不同的團隊做這件事,所以效率很重要。如果有人能帶領我走向正確的方向,我將不勝感激。謝謝!將獨立參數傳遞給.mouseover函數
HTML
<div id="content">
<ul class="conference">
<ul class="division">
<li id="nyg" class="team"></li>
<li id="nyj" class="team"></li>
<li class="team"></li>
....
JS
var teams = [
{
id: 'nyg',
name: 'Giants',
city: 'New York',
color1: '#000099',
color2: '#0000ff',
depth: 'http://www.ourlads.com/nfldepthcharts/depthchart/NYG',
offseason: 'http://espn.go.com/nfl/team/transactions/_/name/nyg',
logo: 'logos/nyg.jpeg'
},
{
id: 'nyj',
name: 'Jets',
city: 'New York',
color1: '#006600',
color2: '#009900',
depth: 'http://www.ourlads.com/nfldepthcharts/depthchart/NYG',
offseason: 'http://espn.go.com/nfl/team/transactions/_/name/nyg',
logo: 'logos/nyg.jpeg'
}];
for (team in teams) {
$('#'+teams[team].id;).mouseover(mouseIn(teams[team].color1)).mouseout(mouseOut(teams[team].color2));
}
function mouseIn(color) {
$(this).css("background-color", color);
}
function mouseOut(color) {
$(this).css("background-color", color);
}
? :#my-id-one {background-color:#123456}; #my-id-one:hover {background-color:#654321}; –