今天我正在缩短我的旧代码,我看到了这段代码:
$(".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>");
这段代码也可以工作,但我不知道为什么会这样。在我看来,这段代码不应该工作,因为类“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.
关于javascript - JQuery 如何处理 getElementsByClassName 迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28051474/