jsthishtml
1. javascript this[i]
this[i] 相当于类的Marray属性,javascript中类的属性可直接定义。
2. js下的this有哪些用法
this是Javascript语言的一个关键字。
它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。比如,
function test(){
this.x = 1;
}
随着函数使用场合的不同,this的值会发生变化。但是有一个总的原则,那就是this指的是,调用函数的那个对象。
下面分四种情况,详细讨论this的用法。
情况一:纯粹的函数调用
这是函数的最通常用法,属于全局性调用,因此this就代表全局对象Global。
请看下面这段代码,它的运行结果是1。
function test(){
this.x = 1;
alert(this.x);
}
test(); // 1
为了证明this就是全局对象,我对代码做一些改变:
var x = 1;
function test(){
alert(this.x);
}
test(); // 1
运行结果还是1。再变一下:
var x = 1;
function test(){
this.x = 0;
}
test();
alert(x); //0
情况二:作为对象方法的调用
函数还可以作为某个对象的方法调用,这时this就指这个上级对象。
function test(){
alert(this.x);
}
var o = {};
o.x = 1;
o.m = test;
o.m(); // 1
情况三 作为构造函数调用
所谓构造函数,就是通过这个函数生成一个新对象(object)。这时,this就指这个新对象。
function test(){
this.x = 1;
}
var o = new test();
alert(o.x); // 1
运行结果为1。为了表明这时this不是全局对象,我对代码做一些改变:
var x = 2;
function test(){
this.x = 1;
}
var o = new test();
alert(x); //2
运行结果为2,表明全局变量x的值根本没变。
情况四 apply调用
apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象。因此,this指的就是这第一个参数。
var x = 0;
function test(){
alert(this.x);
}
var o={};
o.x = 1;
o.m = test;
o.m.apply(); //0
apply()的参数为空时,默认调用全局对象。因此,这时的运行结果为0,证明this指的是全局对象。
如果把最后一行代码修改为
o.m.apply(o); //1
运行结果就变成了1,证明了这时this代表的是对象o。
3. javascript可以用this取得html元素值吗
<inputtype='text'onblur='check(this)'>
<script>
functioncheck(e){
console.log(e.value);
}
</script>
4. 在html事件属性中调js函数, this问题
this为当前的Object。在你的函数
function demo() {
alert(this.id)
this.value = a++;
}
中this指向的是demo而非触发它的input
所以
this要传递的。
不知你是否想要下面的
<body>
<input type="button" id="aButton" value="A" onclick="demo(this)" />
<input type="button" id="bButton" value="B" />
<script type="text/javascript">
var a=1;
function demo(t) {
alert(t.id)
t.value = a++;
}
document.getElementById("bButton").onclick = new Function("demo(this)");
var button_a = document.getElementById("aButton");
alert(button_a.onclick);
var button_b = document.getElementById("bButton");
alert(button_b.onclick);
</script>
</body>
5. 还是关于js中this的问题,代码如下
this指的是调用该函数的对象的引用
6. javascript的html事件中this的问题
this是指本元素
每一个HTML标签,都会在DOM树下产生一个元素节点,这个this是写在哪个标签内,那么show函数里面的参数就是哪个标签所产生的元素的id,如
<input type="button" id="我是button1" onclick="alert(this.id)" value="按我测试"/>
<input type="button" id="我是button2" onclick="alert(this.id)" value="按我测试"/>
7. html js函数传的this是什么意思
意思就是把你触发事件的这个控件传递过去.
<input type="button" id="tianjia" value="保回 存" class="btn1" onClick="nullCheck(this)"/>
function nullCheck(obj){
obj.value;//这样就得到控答件value了
}
8. JS中,为什么不能直接写"url("this.src")"而一定要写成"url("+this.src+")"详细代码见下图
你可以看成如下:
"url(":一个字符串
this.src:一个字符串
")":一个字符串
然后再用内+连接起来,组成一个新容的字符串。。。
这样你明白了么???
var a="url(";
var b=this.src;
var c=")";
var d=a+b+c;
9. html js函数传的this是什么意思
意思就是把你触发事件的这个控件传递过去.
<input type="button" id="tianjia" value="保 存" class="btn1" onClick="nullCheck(this)"/>
function nullCheck(obj){
obj.value;//这样就得到控件value了
}