A标签中通过href和onclick传递的this对象
2016-11-12 23:07
393 查看
功能:删除表格中的一行
body:
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:void(0);" onclick="delRow(this);">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
js:
// 创建删除函数
function delRow(obj){
var node=obj.parentNode.parentNode;
node.parentNode.removeChild(node);
}
一开始是这样写的:<a href="javascript:delRow(this);">删除</a>,发现传过去的不是a标签。
上网查询发现是这样的:
当使用onclick="delRow(this)"的时候,解释器会给他包装一个匿名函数,变成了:
a.onclick = function anonymous()
{
delRow(this);
}
这个this指的就是a这个对象,而使用href的方式时,由于是一个地址,这个this就无处可指了。
body:
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:void(0);" onclick="delRow(this);">删除</a></td> <!--在删除按钮上添加点击事件 -->
</tr>
</table>
js:
// 创建删除函数
function delRow(obj){
var node=obj.parentNode.parentNode;
node.parentNode.removeChild(node);
}
一开始是这样写的:<a href="javascript:delRow(this);">删除</a>,发现传过去的不是a标签。
上网查询发现是这样的:
当使用onclick="delRow(this)"的时候,解释器会给他包装一个匿名函数,变成了:
a.onclick = function anonymous()
{
delRow(this);
}
这个this指的就是a这个对象,而使用href的方式时,由于是一个地址,这个this就无处可指了。
相关文章推荐
- A标签中通过href和onclick传递的this对象实现思路
- A标签中通过href和onclick传递的this对象
- A标签中通过href和onclick传递的this对象
- A标签中通过href和onclick传递的this对象实现思路
- A标签中通过href和onclick传递的this对象实现思路
- A标签中通过href和onclick传递的this对象
- A标签中通过href和onclick传递的this对象
- <a>标签中href和onclick传递this对象的实现方法
- <a>标签中 href 和 onclick 的区别,以及href="javascript:xxx(this);"与onclick="xxx(this);" 传递this参数的区别
- 通过给事件处理程序传递this参数,获取事件源对象的引用。单机提交按钮时在信息框中显示用户输入的字符。
- a标签href属性传递参数,onclick属性传递参数
- <a>标签使用onclick(this)来传递参数
- <a href="onclick="javascript:goSearch(this)" class="click" name="Java">Java</a>为什么a标签的父节点获取不到
- QTP的那些事---通过html标签的属性获取对象(类似onclick的属性)
- href与onclick()分别添加this对象
- a标签的href和onclick的区别
- 如何通过序列化在网络间传递对象,网络协议:轻松定义自己的网络通讯协议
- 通过超链接标签传递文本框的值
- Android 通过intent 传递对象