jQuery和'this'对象 [英] JQuery and 'this' object

查看:88
本文介绍了jQuery和'this'对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下JQuery函数附加到网页上n个文本框的模糊事件.

$(document).ready(function() {
        $("input[id$='_txtTuitionAmt']").blur(function() {
            alert(this.value);
        })
    });

效果很好.当用户从任何文本框中跳出时,都会弹出警报,并在文本框中显示该值.

我感到困惑的是"this.value",以及它是JQuery还是JavaScript.我是否以正确的方式使用了"this"对象,还是应该做其他事情以获取元素的值?

对不起,如果我的问题似乎有点模糊.我只是想着手处理这个"对象及其工作方式.我查看了JQuery文档,但在"this"上找不到任何内容.

解决方案

引用当前的dom对象(即document.getElementById("someid")引用所需的dom对象的方式相同).现在,基于浏览器,您可以访问该对象的功能/字段(即this.nodeName, this.value,...).您正在访问浏览器的实现所提供的功能.

如果使用 $(this)(或$("#someid")$(document.getElementById("someid"))),则将对象封装在jquery中-因此,您现在可以访问jquery函数和字段(即,....)

如果您有一个jquery对象(即var n = $(this).find("#someid");),并且想要摆脱jquery封装,因为您需要一个标准的dom函数,则可以使用 .get(0). /p>

本身可以解析为不同的对象,具体取决于调用该对象的位置.如果在onclick或其他事件处理程序(<div id="asdf" onclick="alert(this.id)...-将打印 asdf ),表单或其他对象中调用,则它可以是一个节点-请参见

It works just fine. When the user tabs out of any of the textboxes then an alert popups and displays the value within the textbox.

What I'm confused about is "this.value," and whether it is JQuery or JavaScript. Am I using the 'this' object in the correct manner, or should I be doing something else in order to get at the value of the element?

Sorry if my question seems a bit murky. I'm just trying to come to grips with the "this" object and how it works. I looked in the JQuery documentation, but couldn't find anything on "this".

解决方案

this refers to the current dom object (the same way ie document.getElementById("someid") refers to the wanted dom object). Based on the browser you can now access functions/field of that object (ie. this.nodeName, this.value, ...) You are accessing what is provided by the browser's implementation.

If you use $(this) (or $("#someid") or $(document.getElementById("someid"))) You are ecapsulating the object in jquery - thus you can now access the jquery functions and fields (ie. $(this).val(); $(this).find("somenode"), ....)

If you have a jquery object (i.e. var n = $(this).find("#someid"); ) and you want to get rid of the jquery capsule, because you need a standard dom function you get use .get(0).

this itself can resolve to different objects, depending on where it is called. It can be a node if called within an onclick or other event handler (<div id="asdf" onclick="alert(this.id)... - will print asdf), the form or some other object - see http://www.quirksmode.org/js/this.html

这篇关于jQuery和'this'对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆