2014-02-05 122 views
1

我需要幫助理解爲什麼這些東西的行爲奇怪:jQuery選擇的煩惱

alert($('div.entry').text());     returns some long text 
alert(Thesaurus.options.containers);   returns string div.entry 
alert($(Thesaurus.options.containers).text()); breaks with Uncaught RangeError: Maximum call stack size exceeded 

的HTML有下幾個div.entry元素500個字。

的Thesaurus.options.containers看起來是這樣的:

jQuery.Thesaurus({ 
     caseSentitive: false, 
     zetind: 'auto', 
     delay: 250, 
     containers: ['div.entry'], 
     effect: 'slide', 
... 
+1

如果你叫什麼'$(Thesaurus.options.containers [0])文本()' –

+0

硬盤沒有看到你使用這個對HTML進行調試。 – j08691

+0

最好使用'console.log()'而不是'alert()'。 'console.log()'會告訴你'Thesaurus.options.containers'包含一個單個字符串的數組。 'alert()'用逗號加入了數組中的值,在你的情況下它看起來像是一個字符串。 –

回答

1

所以,基本上,你這樣做是:$(['div.entry]).text();我猜,因爲你傳遞你調用這樣的數組: http://api.jquery.com/jQuery/#jQuery-elementArray這意味着採取一組元素,而不是一個選擇器數組。你可以在這裏看到這個炸彈:http://jsfiddle.net/dE9Yb/

你可以做的,而不是這樣的:

alert($(Thesaurus.options.containers.join(",")).text()); 

因此,通過在一個字符串,它是用逗號加入了一個選擇。

參見: http://jsfiddle.net/dE9Yb/1/