我是Javascript和Jquery的新手,所以请原谅这是一个愚蠢的问题
我有一个HTML,如图所示
<div id="restmenu" class="restMenu">
<ul>
<section id="Home" class="ulseWrap lielement">
<div class="intit someclassss">Home<span id="deleteHome" class="inDelete"></span><span class="inEdit" title="Modify Address" name="Modify Address"></span></div>
</section>
<section id="Office" class="ulseWrap lielement">
<div class="intit someclassss">Office<span id="deleteOffice" class="inDelete"></span><span class="inEdit" title="Modify Address" name="Modify Address"></span></div>
</section>
</ul>
</div>
我是这样尝试的
var existing = $("#restmenu").find('ul').map(function () {
return $(this).attr("id");
}).get();
alert(existing);
但是这产生了一个空洞的警报。
答案 0 :(得分:3)
试
在您的代码$("#restmenu").find('ul')
中找到ul
而不是section
var existing = $("#restmenu").find('ul section').map(function () {
return $(this).attr("id");
}).get();
alert(existing);
答案 1 :(得分:0)
还有一个jsfiddle - http://jsfiddle.net/Arindamnayak/74axeyud/
<div id="restmenu" class="restMenu">
<ul>
<section id="Home" class="ulseWrap lielement">
<div class="intit someclassss">Home<span id="deleteHome" class="inDelete"></span><span class="inEdit" title="Modify Address" name="Modify Address"></span></div>
</section>
<section id="Office" class="ulseWrap lielement">
<div class="intit someclassss">Office<span id="deleteOffice" class="inDelete"></span><span class="inEdit" title="Modify Address" name="Modify Address"></span></div>
</section>
</ul>
</div>
<input type="button" value="exec" onclick="getdate()" />.
JS是
function getdate()
{
var val = "";
console.log( $("#restmenu ul"));
$("#restmenu ul section").each(function( index ) {
val = val + $(this).attr("id") + " ";
});
alert(val);
}