JQuery 如何处理 getElementsByClassName 迭代?

How JQuery handles with getElementsByClassName iteration?

今天我在缩短旧代码时看到了这段代码:

$(".ceker").append("<a href='kit/");
$("#pdf").append("pdf");
$("#txt").append("txt");
$("#epub").append("epub");
$(".ceker").append("/7.");
$("#pdf").append("pdf");
$("#txt").append("txt");
$("#epub").append("epub");
$(".ceker").append("'><div class='menukitap'><div class='menuharry7 menuharresler'></div><h4>Ölüm Yadigarları İndir</h4></div></a>");

以前是这样输出的:

<a href="kit/pdf/7.pdf"><div class="menukitap"><div class="menuharry7 menuharresler"></div><h4>Ölüm Yadigarları İndir</h4></div></a>

然后我把它缩短成这样:

function item()
{
    if( $('.ceker').attr('id') == 'pdf' )
    {
        return "pdf";
    }
    if( $('.ceker').attr('id') == 'txt' )
    {
        return "txt";
    }
    if( $('.ceker').attr('id') == 'epub' )
    {
        return "epub";
    }
}

$(".ceker").append("<a href='kit/" + item() + "/7." + item() + "'><div class='menukitap'><div class='menuharry7 menuharresler'></div><h4>Ölüm Yadigarları İndir</h4></div></a>");

此代码也有效,但我不明白为什么会这样。在我看来,这段代码不应该工作,因为 class "ceker" 已经有 3 个不同的 ID "pdf" 'txt' 和 "epub"

所以这是我的问题:JQuery 如何处理 getElementsByClassName Iteration(答案 1)? (我不明白这是怎么回事if( $('.ceker').attr('id') == 'pdf' )

这里是HTML如果你需要

<nav class="menu">
                    <span class="ara-menu-yer">Türkçe</span>
                    <a><li><section id="pdf" class="ceker"></section></li></a>
                    <a><li><section id="txt" class="ceker"></section></li></a>
                    <a><li><section id="epub" class="ceker"></section></li></a>
                    <span class="ara-menu-yer">English</span>
                    <a><li><section id="ingpdf" class="cekereng"></section></li></a>
                    <a><li><section id="ingdoc" class="cekereng"></section></li></a>
                </nav>

如果有拼写、语法或词汇错误,我深表歉意。我想我无法解释我的问题,但我希望你能理解。

来自the docs

Get the value of an attribute for the first element in the set of matched elements.