在table表格 中点击置顶的js脚本

如题所述

使用Jquery吧,希望可以帮到你:
<table id="tb1" style="width:200px;">
<tr><td>1</td><td><a href="javascript:void(0);" onclick="SetTop(this);">置顶</a></td></tr>
<tr><td>2</td><td><a href="javascript:void(0);" onclick="SetTop(this);">置顶</a></td></tr>
<tr><td>3</td><td><a href="javascript:void(0);" onclick="SetTop(this);">置顶</a></td></tr>
<tr><td>4</td><td><a href="javascript:void(0);" onclick="SetTop(this);">置顶</a></td></tr>
<tr><td>5</td><td><a href="javascript:void(0);" onclick="SetTop(this);">置顶</a></td></tr>
</table>
<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
function SetTop(v){
var tr = $(v).parent("td").parent("tr");
$("#tb1").find("tr").first().before(tr);
}
</script>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-28
var objTbody=document.getElementById('news_table').getElementsByTagName('tbody')[0];
//objTbody主要是为了找到表格的tbody
var obj1=objTbody.childNodes[?];//?为你要置顶的那个tr的index,如果你要第二个置顶则填1
objTbody.removeChild(obj1);//从tbody里移除这个tr

objTbody.innerHTML=obj1.innerHTML+objTbody.innerHTML//加到最前面

如果有jq就更简单了追问

谢谢 ,还有其他答案吗?

追答

先不看代码,你先想一想,置顶无非就是把你要置顶的那个tr拿出来,然后搞到最上面
接下来就是实现代码了