我在HTML頁面上有幾個具有相同類的元素-但是它們是不同的元素類型。我想在遍歷元素時找出元素的標簽名稱-但是.attr不會使用“標簽”或“標簽名稱”。這就是我的意思??紤]頁面上的以下元素:<h1 class="rnd">First</h1><h2 id="foo" class="rnd">Second</h2><h3 class="rnd">Third</h3><h4 id="bar" class="rnd">Fourth</h4>現在,我想運行類似的代碼,以確保所有元素都具有一個ID(如果尚未定義):$(function() { $(".rnd").each(function(i) { var id = $(this).attr("id"); if (id === undefined || id.length === 0) { // this is the line that's giving me problems. // .attr("tag") returns undefined $(this).attr("id", "rnd" + $(this).attr("tag") + "_" + i.toString()); } });});我想要的結果是H2和H4元素的ID為rndh2_1rndh4_3分別。關于如何發現“ this”表示的元素的標簽名稱的任何想法?javascript jquery
jQuery可以提供標簽名稱嗎?
慕沐林林
2019-11-30 12:49:02