手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 免费部署 N8N 的 Zeabur 注册 | 登陆
浏览模式: 标准 | 列表全部文章

JQuery — this 和 $(this) 的区别

在美拓的BLOG【http://meito.22web.net/?p=51】上面看到这篇文章,先说说我的理解吧。

jQuery的代码中,this是代表了当前对象。例如:$("#test").click(function(){ alert(this.value )});,在这个方法里,如果用了this,那就是相当于直接使用了 test 元素这个对象,有点象document.getElementById("#test")一样【说的我自己都迷糊了。。。】在这个方法中的this,就是ID为test的元素本身

而$(this),则是把这个元素对象重新进行了 jQuery的包装。。。

说的太乱了。。。。直接看美拓的原文吧。

-------原文开始-------------

起初以为this和$(this)就是一模子刻出来。但是我在阅读时,和coding时发现,总不是一回事。
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.

JavaScript代码
  1. $("#textbox").hover(  
  2.       function() {  
  3.            this.title = "Test";  
  4.       },  
  5.       fucntion() {  
  6.           this.title = "OK”;  
  7.       }  
  8. );  

 

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

Error Code:

JavaScript代码
  1. $("#textbox").hover(  
  2.        function() {  
  3.           $(this).title = "Test";  
  4.        },  
  5.        function() {  
  6.           $(this).title = "OK";  
  7.        }  
  8. );  

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

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

正确的代码:

JavaScript代码
  1. $("#textbox").hover(  
  2.       function() {  
  3.          $(this).attr(’title’, ‘Test’);  
  4.       },  
  5.       function() {  
  6.          $(this).attr(’title’, ‘OK’);  
  7.       }  
  8. );  
使用JQuery的好处是它包裝了各种浏览器版本对DOM对象的操作,因此统一使用$(this)而不再用this应该是比较不错的选择。

---EOF---

看来还是不行啊我。。语言组织能力太差

 

 

Tags: jquery

phpQuery

想分析网页?不会写正则?这。。。一切都不是问题

在以前是不可想象的。但如今google的项目里有这个phpQuery,它可以让一切变得可能。。。

phpQuery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on jQuery JavaScript Library.

Library is written in PHP5 and provides additional Command Line Interface (CLI).

如果你使用过jQuery,你会发现这一切是如此的相象。

PHP代码
  1. <?php  
  2.   
  3. include_once'./phpQuery.php' );  
  4.   
  5. $html = new phpQueryDocumentFile('xxx.html');  
  6. $title = $html->find('title')->text();  
  7.   
  8. echo $title;  
以上内容是取得网页的 title。多方便啊。。。

如果是取得某个class的内容呢?

PHP代码
  1. <?php  
  2.   
  3. //...include   
  4. //...new phpQueryDocument  
  5.   
  6. $html->find('div.test eq(1)')->html();  
  7. //表示取得div的class为test的,第一个匹配的元素中html内容  
呵呵,快去:http://code.google.com/p/phpquery/下载吧

Tags: phpquery

告诉自己。。。

告诉自己,生日快乐
又是一年过去了。又老了一岁了。。
农历生日还是很好记的嘛。。呵呵
怀旧,复古。
很多人不理解。自己理解一下就行了。。。

欲望。。。

欲望其实是动力的来源,就看你是否有想把欲望转化为动力了。。

目前的欲望是:

http://www.jj.js.cn/travel/index.html

http://www.jj.js.cn/food/index.html

以及

http://www.jj.js.cn/jjsp/spindex.html

短时间内的欲望。。。

把页面先记录下来,呵呵,慢慢慢转化为动力先

javascript替换字符

来自司徒正美,比较方便的关键符号替换。

司徒正美认为:
不用多言,这种技术被广泛应用于表单验证,语法高亮和危险字符过滤中。一段话如果很长,如果不想像下面那样替换,我们得想些办法了。

他先给了个简单的例子:

JavaScript代码
  1. var hash = {  
  2.     '<' : '&lt;' ,
        '>' : '&gt;',
        '…' : '&hellip;',
        '“' : '&ldquo;' ,
        '”' : '&rdquo;' ,
        '‘' : '&lsquo;' ,
        '’' : '&rsquo;' ,
        '—' : '&mdash;',
        '–' : '&ndash;'
  3. };  
  4.    
  5. str = str.  
  6.     replace( /&(?!#?\w+;)/g , '&amp;' ).  
  7.     replace( /"([^"]*)"/g   , '“$1”'  ).  
  8.     replace( /[<>…“”‘’—–]/g , function ( $0 ) {  
  9.         return hash[ $0 ];  
  10.     });  

并表示:缺陷也很明显,如哈希的键必须是简单的普通字符串,不能是复杂正则,这就是我们不得不分开的原因。replace在老一点的浏览器是不支持function的。为此,我们只好放弃上面最后那个replace方式,替换方统一为普通字符串。

于是,他扩展的String的基类,添加了一个方法:

JavaScript代码
  1. String.prototype.multiReplace = function ( hash ) {  
  2.     var str = this, key;  
  3.     for ( key in hash ) {  
  4.         if ( Object.prototype.hasOwnProperty.call( hash, key ) ) {  
  5.             str = str.replace( new RegExp( key, 'g' ), hash[ key ] );  
  6.         }  
  7.     }  
  8.     return str;  
  9. };  

并给出了实现代码:

JavaScript代码
  1. str = str.multiReplace({  
  2.     '&(?!#?\\w+;)' :'&amp;',
        '"([^"]*)" : '“$1”',
        '<' : '&lt;' ,
        '>' : '&gt;',
        '…' : '&hellip;',
        '“' : '&ldquo;' ,
        '”' : '&rdquo;' ,
        '‘' : '&lsquo;' ,
        '’' : '&rsquo;' ,
        '—' : '&mdash;',
        '–' : '&ndash;'

  3. });  

Look,多简单啊。如果觉得我这里的表示更简单的话,请看原文http://www.cnblogs.com/rubylouvre/archive/2009/10/12/1581094.html