편집 : 당신이 IE6 지원에 대한 관심을하지 않으면, 당신은 배경을 변경하기 위해 CSS :hover
의사 선택을 사용할 수 참고. 이것은 아마도 첫 번째 고려 사항입니다.
function addRecentData(data) {
$('#newTable tr:last').after('<tr><td class="name"></td><td class="id"></td></tr>');
var $tr = $('#newTable tr:last');
$tr.find('.name').html(data.Name);
$tr.find('.id').html(data.Id);
$tr.mouseover(function() {
$(this).css('backgroundColor','#E1EBF4');
// this.style.backgroundColor = '#E1EBF4'; // or you could do this
});
}
또 다른 방법 대신 after()
의 inserAfter()
를 사용하는 것, 바로 변수를 지정 : 현재 코드 감안할 때
#newTable tr:hover {
background: #E1EBF4;
}
, 당신은 테이블에 $tr
참조를 사용할 수 있습니다.
function addRecentData(data) {
var $tr = $('<tr><td class="name"></td><td class="id"></td></tr>')
.insertAfter('#newTable tr:last');
$tr.find('.name').html(data.Name);
$tr.find('.id').html(data.Id);
$tr.mouseover(function() {
$(this).css('backgroundColor','#E1EBF4');
// this.style.backgroundColor = '#E1EBF4'; // or you could do this
});
}
또는 각 <tr>
는 마우스 오버를 받아야하는 경우, 당신은 마우스 오버 돌봐하기 위해 #newTable
에 .delegate()
를 사용할 수
.
이제 <tr>
요소는 테이블에 추가 될 때 원하는 기능을 자동으로 가져옵니다.
$ (this) .css ('backgroundColor', '# E1EBF4'); 나를 위해 너무 잘 작동하지 않았다. 다른 옵션은 훌륭하게 작동했습니다. 감사! – MrM