我不知道你的HTML
代碼,但這裏的追加和刪除FIDDLE
HTML
<table>
<tr>
<td>1</td>
<td><button class="addtofavs">+</button></td>
<td>Name</td>
</tr>
<tr>
<td>2</td>
<td><button class="addtofavs">+</button></td>
<td>Name</td>
</tr>
<tr>
<td>3</td>
<td><button class="addtofavs">+</button></td>
<td>Name</td>
</tr>
<tr>
<td>4</td>
<td><button class="addtofavs">+</button></td>
<td>Name</td>
</tr>
<tr>
<td>5</td>
<td><button class="addtofavs">+</button></td>
<td>Name</td>
</tr>
</table>
<div id="favorite"></div>
CSS
table {
position: relative;
float: left;
border: 1px solid #ddd;
border-collapse: collapse;
}
td {
height: 22px;
padding: 0 5px;
line-height: 22px;
border: 1px solid #ddd;
}
td:first-child {
width: 15px;
text-align: center;
}
div {
position: relative;
float: left;
width: 150px;
height: 133px;
letter-spacing: 2px;
border: 1px solid #ddd;
}
.fav {
display: block;
float: left;
width: 25px;
height: 25px;
margin: 2px;
line-height: 25px;
text-align: center;
cursor: pointer;
border: 1px solid #ccc;
}
.rem {
background: #ddd;
position: absolute;
display: block;
margin-top: -17px;
margin-left: 20px;
padding: 0 3px;
font-size: 10px;
border-radius: 4px;
}
例
jQuery的
$(function() {
$('.addtofavs').on('click', function() {
$('#favorite').append('<span class="fav">'+$(this).parent('td').prev().text()+'</span>');
});
$(document).on('mouseover','.fav', function() {
$(this).prepend('<span class="rem">Remove</span>');
}).on('mouseleave','.fav', function() {
$('.rem').remove();
});
$(document).on('click','.rem', function() {
$(this,'.rem').parent().remove();
});
});
請出示你的全部代碼。 –
顯示html的簡化版本 – Huangism