<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script>
<!--
function getVById(){
var tt=this.id;
alert(tt);
}
--></script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
</body>
<html>HTMLthis
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script>
<!--
function getVById(){
var tt=this.id;
alert(tt);
}
--></script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
</body>
<html>HTMLthis
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script>
<!--
function getVById(obj){
var tt=obj.id;
alert(tt);
}
-->
</script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById(this)" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById(this)"/>测试2
</body>
<html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript"> window.onload= function(){
var atr = document.getElementsByTagName('input');
//alert('0');
for(i=0;i<atr.length;i++)
{ atr[i].onclick=function (){
var tt=this.id;
alert(tt);
}
}</script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
</body>
<html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript"> window.onload= function(){
var atr = document.getElementsByTagName('input');
//alert('0');
for(i=0;i<atr.length;i++)
{ atr[i].onclick=function (){
var tt=this.id;
alert(tt);
}
}</script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
</body>
<html>这里我还是直接用this 但又行了 这是什么原因啊 也是直接在函数里面调用的
你并没有调用 getVById()方法
通过动态赋予onclick事件已经将onclick="getVById()" 失效了
atr[i].onclick 通过this 就是指atr[i]对象 也即当前 radio
你并没有调用 getVById()方法
通过动态赋予onclick事件已经将onclick="getVById()" 失效了
atr[i].onclick 通过this 就是指atr[i]对象 也即当前 radio
这句还是不明白:atr[i].onclick 通过this 就是指atr[i]对象 也即当前 radio? this不是指向window ?
然后再在atr[i].onclick=function (){...}中alert一下参考下这篇博客和其中评论
http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript"> window.onload= function(){
var atr = document.getElementsByTagName('input');
//alert('0');
for(i=0;i<atr.length;i++)
{ atr[i].onclick=function (){
var tt=this.id;
alert(tt);
}
}</script>
</head>
<body>
<input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
<input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
</body>
<html>这里我还是直接用this 但又行了 这是什么原因啊 也是直接在函数里面调用的 window.onload= function(){
var atr = document.getElementsByTagName('input');
//alert('0');
for(i=0;i<atr.length;i++)
{
atr[i].onclick=function (j){
return function() {
var tt=this.id;
alert(tt);
}
}(i);
}
1#的方法中:
function getVById(){
var tt=this.id;
alert(tt);
}
这个方法与你的triggerElement完全没有任何联系, 它里面的this始终指向的是window.
2#的方法
function getVById(obj){
var tt=obj.id;
alert(tt);
}getVById(this);
在点击时, 将triggerElement传入了函数, obj即那个按钮, 可以获取其属性
后面的方法:
atr[i].onclick=function (){
var tt=this.id;
alert(tt);
}
中atr[i]即为按钮. 相当于为每个[按钮实例]添加了一个onclick方法. 这个function()只存在于这个实例中, 在响应click事件时, function内部的this全部指向按钮实例
你用getElementsByTagName("input")是获取的文档中所有的input对象(即是一个input元素数组),然后用for循环时就是一个input对象(或者说是一个button对象),用this的话就是指的这个button对象了。