2012-01-05 67 views
-1

可能重複:
What does this mean in jquery $('#id', javascript_object);
What does $(''class for the same element', element) mean?
What does the second argument to $() mean?

任何人都知道什麼是:

$('element', $$).function(){...}; 

seen here

而且

$('element', this).function(){...}; 

seen here

+0

除了[文件]的信息(http://api.jquery.com/jQuery/#jQuery1)你需要什麼? – 2012-01-05 14:33:10

+0

可能的重複[這是什麼意思在jquery $('#id',javascript_object);](http://stackoverflow.com/questions/3262974/what-does-this-mean-in-jquery-id-javascript - 對象)和[什麼$(''類爲相同的元素,元素)是什麼意思?](http://stackoverflow.com/questions/6479300/what-does-class-for-the-same-element-元素的意思)和[什麼第二個參數$()是什麼意思?](http://stackoverflow.com/questions/6979097/what-does-the-second-argument-to-mean)。 – 2012-01-05 14:34:43

+0

從doc:「在內部,選擇器上下文是用.find()方法實現的,所以$('span',this)相當於$(this).find('span')。」 – 2012-01-05 14:35:10

回答

5
$('.pblabel', this).text(newVal + '%'); 

是一回事

$(this).find('.pblabel').text(newVal + '%'); 

事實上,這是它被改寫並在內部運行的方式。它被稱爲「上下文選擇器」。

jQuery source

// HANDLE: $(expr, $(...)) 
} else if (!context || context.jquery) { 
    return (context || rootjQuery).find(selector); 

// HANDLE: $(expr, context) 
// (which is just equivalent to: $(context).find(expr) 
} else { 
    return this.constructor(context).find(selector); 
} 
+0

這很清楚,謝謝。 – 2012-01-05 14:38:25

5

它使用this$$作爲上下文,即返回的所有元素都必須是其後代。默認值是document

+0

'$$'='這個'? – 2012-01-05 14:39:30

+0

@GG。不,它可能是任何變量。 '$'在變量和函數名稱中是有效的(因此jQuery的主'''函數)。 – Ryan 2012-01-05 14:40:53

相關問題