我正在用一行代碼來完成此操作。將div ID轉換爲字符串
var a = '';
$('#MyDivs div').each(function(){a+=this.id+',';});
a = a.substring(0,a.length-1);// remove the last comma
我雖然這樣做會做到這一點,但無濟於事。我在正確的軌道上?
$('#MyDivs div[id]').join(', ');
我正在用一行代碼來完成此操作。將div ID轉換爲字符串
var a = '';
$('#MyDivs div').each(function(){a+=this.id+',';});
a = a.substring(0,a.length-1);// remove the last comma
我雖然這樣做會做到這一點,但無濟於事。我在正確的軌道上?
$('#MyDivs div[id]').join(', ');
在這裏,你在一行去:
var a = $('#MyDivs div[id]').map(function(){ return this.id }).get().join(', ');
這裏的小提琴:http://jsfiddle.net/tGFeZ/
不錯, get()轉換爲數組。每天學習新的東西:) – Marlin 2012-01-02 07:31:24
甜,我知道它可以完成。乾杯。 – 2012-01-02 08:01:06
這應該提供一些幫助:How to get all of the IDs with jQuery?
有一個行示例在那裏,雖然它可能不是最有吸引力的解決方案。
謝謝。在發佈之前,我至少搜索了10分鐘的答案。猜猜我需要處理我的搜索條件:/ – 2012-01-02 08:02:05
如果用「單行」表示單個語句,則此庫不可行。如果你只是在尋找一串簡短的陳述,那麼下面就會做到。但是,即使下面應該在多行中表示可讀性
$.makeArray($('#MyDivs div[id]').map(function(){return this.id})).join(',');
下同:
$.makeArray($('#MyDivs div[id]').map(function(){
return this.id
}).join(',');
嘗試$(本).attr( 「ID」),而不是this.id – 2012-01-02 07:16:48
@RajatSinghal' $(this).attr('id')'與'this.id'相同,只不過第一個是jQuery方式。 – Nathan 2012-01-02 07:18:23
可能重複[如何使用jQuery獲取所有ID?](http://stackoverflow.com/questions/827294/how-to-get-all-of-the-ids-with-jquery) – 2012-01-02 08:41:56