前端开发

 首页 > 前端开发 > jquery教程 > jQuery中$this和$(this)的区别介绍

jQuery中$this和$(this)的区别介绍

分享到:
【字体:
导读:
          jQuery中$this和$(this)的区别介绍 //this其实是一个Html元素。//$this只是个变量名,加$是为说明其是个jquery对象。//而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jque...

jQuery中$this和$(this)的区别介绍

// this其实是一个Html 元素。
// $this 只是个变量名,加$是为说明其是个jquery对象。
// 而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
 
 
(function($){
    $.fn.hilight = function(options){
        debug(this);
 
        var defaults = {
            foreground: 'red',
            background: 'yellow'
        };
 
        var opts = $.extend({}, $.fn.hilight.defaults, options);
 
        return this.each(function() {
      // this其实是一个Html 元素。
      // $this 只是个变量名,加$是为说明其是个jquery对象。
      // 而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
            $this = $(this);
 
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
             
            // update element styles
            $this.css({
                backgroundColor: o.background,
                color: o.foreground
            });
 
            var markup = $this.html();
            // call our format function
 
            markup = $.fn.hilight.format(markup);
 
            $this.html(markup);
        });
 
    };
 
 
    // define our format function
    $.fn.hilight.format = function(txt) {
        return '' + txt + '';
    };
 
 
    // 插件的defaults
    $.fn.hilight.defaults = {
        foreground: 'red',
        background: 'yellow'
    };
 
    function debug($obj) {
        if (window.console && window.console.log){
            window.console.log('hilight selection count: ' + $obj.size());
        }
    };
 
})(jQuery)


分享到:
区别jQuery中height与width
query中有三个获取element高度的方法,分别是:height(),innerHeight(),outerHeght(bool);同样对应的有三个获取element宽度的方法:width(),innerHeight(),outerHeight(bool),这三个方法分别对应怎样的元素属性,如下图所示: 从上面的图可以了解到:height()方法对应顶部style设置的width属性; innerHeight()对应width+paddin...
JQuery中如何阻止表单的提交动作
jquery如何阻止submit表单提交呢?请看下面的代码。 在JQuery中阻止表单的提交动作 我们都知道在表单提交时我们可以通过onsubmit属性指定方法在方法中return true或false来决定表单是否提交 但是在JQuery中,我们通过$().subimt(function(){});怎样来实现阻止表单的提交呢? JavaScript: JQuery: $("#myForm"...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……