这是我第一次尝试自己编写 jQuery。我正在努力做到这一点,因此当我将鼠标悬停在一个名称的实例上时,背景以及该名称的所有其他实例都会变为白色。每个名称都有一个唯一的数据 ID,它对所有实例都是通用的。
person = this.data("id");
我认为我的 jQuery 在尝试分配悬停元素的数据属性时出错,然后使用该数据属性更改所有元素的背景。不确定我是否接近。
错误如下:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'data' localhost:60
(anonymous function) localhost:60
jQuery.event.special.(anonymous function).handle jquery.js:3353
jQuery.event.dispatch jquery.js:3062
elemData.handle.eventHandle
<div id="center">
<% @instances.each do |instance| %>
<div class="instance" data-id="<%= instance.person.id %>" style="top:<%=top_helper(instance)%>px; left:<%= left_helper(instance) %>px; width:<%= width_helper(instance) %>px;">
<span><%= instance.person.first_name %>, <%= instance.rank %></span>
</div>
<% end %>
</div>
<script>
$("div#center").ready(function(){
var person
$("div.instance").hover(function(){
person = this.data("id");
$data(person).css("background-color","white");
});
});
</script>