如何获取具有相同类的元素的每个attr id?
Executing tasks: [:app:compileDebugSources, :app:compileDebugAndroidTestSources]
Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:compileDebugAndroidTestJava
:app:compileDebugAndroidTestNdk UP-TO-DATE
:app:compileDebugAndroidTestSources
BUILD SUCCESSFUL
Total time: 5.7 secs
但是当我执行console.log时,它只返回一个id,即使我有多个id和多个具有相同类的div。
答案 0 :(得分:7)
使用map()
var ids = $('.class').map(function() {
return $(this).attr('id');
});
console.log(ids);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div class="class" id="1"></div>
<div class="class" id="2"></div>
<div class="class" id="3"></div>
&#13;
答案 1 :(得分:4)
使用jquery各个函数。
var i = 0;
var ids = [];
$(".class").each(function(){
ids[i++] = $(this).attr("id"); //this.id
});
答案 2 :(得分:1)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.map demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div class="map-test" id="1"></div>
<div class="map-test" id="21"></div>
<div class="map-test" id="31"></div>
<div class="map-test" id="41"></div>
<div class="map-test" id="51"></div>
<div class="map-test" id="61"></div>
<div class="map-test" id="71"></div>
<div class="map-test"></div>
<script>
var arr = jQuery.map(jQuery('.map-test'),function(n,i){
return jQuery(n).attr('id');
});
console.log(arr);
//will print ["1", "21", "31", "41", "51", "61", "71"]
</script>
</body>
</html>
答案 3 :(得分:1)
var ids = $('.class').map(function() {
return this.id;
}).get();
alert(ids);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="class" id="a"></div>
<div class="class" id="b"></div>
<div class="class" id="c"></div>
&#13;