13
jQueryはタグ名を提供できますか?
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") + "_" + …
115
javascript
jquery