Submitted by gouki on 2009, October 15, 9:51 AM
在美拓的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代码
- $("#textbox").hover(
- function() {
- this.title = "Test";
- },
- fucntion() {
- this.title = "OK”;
- }
- );
这里的this其实是一个Html 元素(textbox),textbox有text属性,所以这样写是完全没有什么问题的。
但是如果将this换成$(this)就不是那回事了,Error–报了。
Error Code:
JavaScript代码
- $("#textbox").hover(
- function() {
- $(this).title = "Test";
- },
- function() {
- $(this).title = "OK";
- }
- );
这里的$(this)是一个JQuery对象,而jQuery对象沒有title 属性,因此这样写是错误的。
JQuery拥有attr()方法可以get/set DOM对象的属性,所以正确的写法应该是这样:
正确的代码:
JavaScript代码
- $("#textbox").hover(
- function() {
- $(this).attr(’title’, ‘Test’);
- },
- function() {
- $(this).attr(’title’, ‘OK’);
- }
- );
使用JQuery的好处是它包裝了各种浏览器版本对DOM对象的操作,因此统一使用$(this)而不再用this应该是比较不错的选择。
---EOF---
看来还是不行啊我。。语言组织能力太差
Tags: jquery
Javascript | 评论:2
| 阅读:22871
Submitted by gouki on 2009, October 15, 6:39 AM
想分析网页?不会写正则?这。。。一切都不是问题
在以前是不可想象的。但如今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代码
- <?php
-
- include_once( './phpQuery.php' );
-
- $html = new phpQueryDocumentFile('xxx.html');
- $title = $html->find('title')->text();
-
- echo $title;
以上内容是取得网页的 title。多方便啊。。。
如果是取得某个class的内容呢?
PHP代码
- <?php
-
-
-
-
- $html->find('div.test eq(1)')->html();
-
呵呵,快去:http://code.google.com/p/phpquery/下载吧
Tags: phpquery
PHP | 评论:1
| 阅读:25085
Submitted by gouki on 2009, October 14, 3:20 PM
告诉自己,生日快乐
又是一年过去了。又老了一岁了。。
农历生日还是很好记的嘛。。呵呵
怀旧,复古。
很多人不理解。自己理解一下就行了。。。
Misc | 评论:2
| 阅读:16732
Submitted by gouki on 2009, October 13, 12:26 PM
欲望其实是动力的来源,就看你是否有想把欲望转化为动力了。。
目前的欲望是:
http://www.jj.js.cn/travel/index.html
http://www.jj.js.cn/food/index.html
以及
http://www.jj.js.cn/jjsp/spindex.html
短时间内的欲望。。。
把页面先记录下来,呵呵,慢慢慢转化为动力先
Misc | 评论:0
| 阅读:15604
Submitted by gouki on 2009, October 12, 12:49 PM
来自司徒正美,比较方便的关键符号替换。
司徒正美认为:
不用多言,这种技术被广泛应用于表单验证,语法高亮和危险字符过滤中。一段话如果很长,如果不想像下面那样替换,我们得想些办法了。
他先给了个简单的例子:
JavaScript代码
- var hash = {
- '<' : '<' ,
'>' : '>',
'…' : '…',
'“' : '“' ,
'”' : '”' ,
'‘' : '‘' ,
'’' : '’' ,
'—' : '—',
'–' : '–'
- };
-
- str = str.
- replace( /&(?!#?\w+;)/g , '&' ).
- replace( /"([^"]*)"/g , '“$1”' ).
- replace( /[<>…“”‘’—–]/g , function ( $0 ) {
- return hash[ $0 ];
- });
并表示:缺陷也很明显,如哈希的键必须是简单的普通字符串,不能是复杂正则,这就是我们不得不分开的原因。replace在老一点的浏览器是不支持function的。为此,我们只好放弃上面最后那个replace方式,替换方统一为普通字符串。
于是,他扩展的String的基类,添加了一个方法:
JavaScript代码
- String.prototype.multiReplace = function ( hash ) {
- var str = this, key;
- for ( key in hash ) {
- if ( Object.prototype.hasOwnProperty.call( hash, key ) ) {
- str = str.replace( new RegExp( key, 'g' ), hash[ key ] );
- }
- }
- return str;
- };
并给出了实现代码:
JavaScript代码
- str = str.multiReplace({
- '&(?!#?\\w+;)' :'&',
'"([^"]*)" : '“$1”',
'<' : '<' ,
'>' : '>',
'…' : '…',
'“' : '“' ,
'”' : '”' ,
'‘' : '‘' ,
'’' : '’' ,
'—' : '—',
'–' : '–'
- });
Look,多简单啊。如果觉得我这里的表示更简单的话,请看原文http://www.cnblogs.com/rubylouvre/archive/2009/10/12/1581094.html
Javascript | 评论:1
| 阅读:18211