zoukankan      html  css  js  c++  java
  • JQuery this 和 $(this) 的区别

    What is "this"? 
    In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked. 

    代码如下:

    $("#textbox").hover( 
    function() { 
    this.title = "Test"; 
    }, 
    fucntion() { 
    this.title = "OK”; 

    ); 


    这里的this其实是一个Html 元素(textbox),textbox有text属性,所以这样写是完全没有什么问题的。 
    但是如果将this换成$(this)就不是那回事了,Error–报了。 
    Error Code: 

    代码如下:

    $("#textbox").hover( 
    function() { 
    $(this).title = "Test"; 
    }, 
    function() { 
    $(this).title = "OK"; 

    ); 


    这里的$(this)是一个JQuery对象,而jQuery对象沒有title 属性,因此这样写是错误的。 

    JQuery拥有attr()方法可以get/set DOM对象的属性,所以正确的写法应该是这样: 

    正确的代码: 

    代码如下:

    $("#textbox").hover( 
    function() { 
    $(this).attr('title', ‘Test'); 
    }, 
    function() { 
    $(this).attr('title', ‘OK'); 

    ); 
     

    详谈jQuery中的this和$(this)

    http://www.jb51.net/article/57376.htm

  • 相关阅读:
    16. Vue 登录存储
    JS 10位、13位时间戳转日期
    14.Vue 定义全局函数
    13.Vue+Element UI实现复制内容
    12.Vue+Element UI 获取input的值
    11.Vue安装Axios及使用
    Layui入手
    MongoDB自启动设置
    sql数据统计
    sql查询总结
  • 原文地址:https://www.cnblogs.com/focusxxxxy/p/6388071.html
Copyright © 2011-2022 走看看