I have some like that.
<script src="http://code.jquery.com/jquery-latest.js"></script>
<table>
<tr title="test" id="ex1">
<td><div class="move">Hello</div></td>
<td> </td>
<td> </td>
</tr>
<tr id="ex2">
<td><div class="move">Hello</div></td>
<td> </td>
<td> </td>
</tr> 开发者_如何学JAVA
</table>
<button>Move</button>
<script>
$("button").click(function () {
$(".move").remove();
});
</script>
If I press button, the both div's will moved. But I need to move only one, according to id of tr it laid.
I have only wrong ideas:
$(".move").parent.parent.attr('id', 'ex2').remove();
Thank you so much!
That would be
$("#ex2 .move").remove();
Simple ya? remember that jQuery selectors are the same as CSS selectors.
You could simplify this.
$('.move').parents('tr').remove();
This will find the nearest parent that is also a <tr>
and will remove it.
...wait. So you want to click a single button and then have it remove only the one div with that specific ID?
In that case just use
$('.move').parents('#ex2').remove();
You can use a more specific selector: http://jsfiddle.net/rWDnM/2/
<table>
<tr title="test" id="ex1">
<td><div class="move">Hello</div></td>
<td> </td>
<td> </td>
</tr>
<tr id="ex2">
<td><div class="move">Hello2</div></td>
<td> </td>
<td> </td>
</tr>
</table>
<button id="button1">Move</button>
<button id="button2">Move 2</button>
<script>
$("#button1").click(function () {
$("#ex1 .move").remove();
});
$("#button2").click(function () {
$("#ex2 .move").remove();
});
</script>
Try this: - jsFiffle
$(".move", "#ex2").remove();
What this does is selects the element(s) with css class of move
within all the children elements (context) of the element with id ex2
and then removes them.
Checfk this for more on the jQuery(selector, context)
syntax - http://api.jquery.com/jQuery/
精彩评论