2009-10-07 93 views
114

我有具有相同類別的HTML頁面上的幾個元素 - 但他們是不同的元素類型。我想查找元素的標籤名稱,因爲我循環它們 - 但.attr不採用「標記」或「標記名稱」。jQuery能否提供標籤名稱?

這裏是我的意思。考慮一個頁面上的這些元素:

<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元素分別具有

rndh2_1 
rndh4_3 

我如何能發現由「本」所代表的元素的標記名稱的任何想法?

+1

也許答案就在這裏:http://stackoverflow.com/a/8355251/271103 – 2012-07-26 11:54:58

回答

109
$(this).attr("id", "rnd" + $(this).attr("tag") + "_" + i.toString()); 

應該

$(this).attr("id", "rnd" + this.nodeName.toLowerCase() + "_" + i.toString()); 
+18

你需要使用this.nodeName.toLowerCase(),因爲大多數DOM HTML文檔的表示自動大寫nodeName。 – NickFitz 2009-10-07 15:27:46

+0

this.nodeName和this.tagName似乎適用於我。謝謝大家! – BigPigVT 2009-10-07 15:50:59

+5

+1用於提及nodeName。有時候,jQuery太過於一步了:-) – 2011-07-05 09:46:20

165

你可以試試這個:

if($(this).is('h1')){ 
    doStuff(); 
} 

更多關於是見docs()。

+11

可能會關注的人:如果您對我的答案贊成不滿,請告訴我爲什麼我可以改進它併爲將來的答案進行學習。謝謝。 – middus 2011-02-13 21:25:21

+3

我也討厭它。無論如何,我upvoted因爲'nodeName'返回一個字符串,這取決於瀏覽器,是否上限或不。 – 2011-04-14 19:49:54

+2

假設以下內容:您不只是有一小部分可能的標記,但它可以是任何100 + html標記。然後你需要寫出:$(this).is('sometag')100多次。我認爲這就是爲什麼有些人低估了你的答案。 – ximi 2012-03-01 20:09:11

3

是。你可以使用下面的代碼:

this.tagName 
53

自從我前一次打這個問題並沒有幫助我在我的情況(我沒有一個this,而是有一個jQuery選擇實例) 。調用get()會爲您提供HTML元素,您可以通過該元素獲取nodeName,如上所述。

this.nodeName; // In a event handler, 'this' is usually the element the event is called on 

$('.hello:first-child').get(0).nodeName; // Use 'get' or simply access the jQuery Object like an array 
$('.hello:first-child')[0].nodeName;  // will get you the original DOM element object 
+1

你是男人! – 2011-05-12 15:08:14

+1

頁面上的第一個幫助 – Rooster 2012-07-19 19:57:51

+0

正是我在找什麼,非常有幫助 - 謝謝:-) – TonyR 2012-08-24 12:33:38

46

你也可以,如果你使用jQuery 1.6或更高版本使用 $(this).prop('tagName');

+0

這正是我所需要的。在我的設計中,我使用了jquery元素,但不是原始的HTML DOM元素。這對我有用。 – Gilthans 2012-05-12 19:56:13

+0

我認爲這是回答這個問題的確切答案。 – CodeTweetie 2013-03-08 11:56:14

1

我認爲你不能在jQuery中使用nodeName,因爲nodeName是一個DOM屬性,而jQuery本身沒有nodeName函數或屬性。但是根據誰第一次提到這個nodeName東西答辯,這是我如何能夠解決這一問題:

this.attr("id", "rnd" + this.attr("nodeName") + "_" + i.toString()); 

注:this這裏是一個jQuery對象。

0

因爲這是你使用jQuery的標記名的第一個孩子作爲查詢我會發布另一個例子在谷歌一起去了一個問題:

<div><p>Some text, whatever</p></div> 

$('div').children(':first-child').get(0).tagName); // ...and not $('div:first-child')[...] 

jQuery的結果爲(大寫)標記名:P

0

考慮快速FILTER方法:

$('.rnd').filter('h2,h4') 

回報:

[<h2 id=​"foo" class=​"rnd">​Second​</h2>​, <h4 id=​"bar" class=​"rnd">​Fourth​</h4>​] 

這樣:

$('.rnd').filter('h2,h4').each(function() { /*...$(this)...*/ }); 
0

你可以得到整個頁面的HTML元素的標籤名。

你可以使用:

 $('body').contents().on("click",function() { 
      var string = this.tagName; 
      alert(string); 
     }); 
0
you can try: 
jQuery(this).get(0).tagName; 
or 
jQuery(this).get(0).nodeName; 

注: 您選擇(H1,H3或......)

0

我纔剛剛寫了另外一個問題,並認爲這本替換也可以幫助你們中的任何一個。

用法:

  • onclick="_DOM_Trackr(this);"

參數:

  1. DOM-對象跟蹤
  2. 回報/警報開關(可選,默認值=警報)

源代碼:

function _DOM_Trackr(_elem,retn=false) 
{ 
    var pathTrackr=''; 
    var $self=$(_elem).get(0); 
    while($self && $self.tagName) 
    { 
     var $id=($($self).attr("id"))?('#'+$($self).attr("id")):''; 
     var $nName=$self.tagName; 
     pathTrackr=($nName.toLowerCase())+$id+((pathTrackr=='')?'':' > '+(pathTrackr)); 
     $self=$($self).parent().get(0); 
    } 
    if (retn) 
    { 
     return pathTrackr; 
    } 
    else 
    { 
     alert(pathTrackr); 
    } 
} 
+0

示例輸出:'html> body> div#coreApp> div#productpage> div> div> div> div#pthumb12> form#thumb_uploader_src>輸入' – 2013-05-24 16:57:16

+0

**另一個用法示例:** '$( '*')每個(函數(_i,_E){$(_ E).attr( '標題',_ DOM_Trackr(_E,真));});' – 2013-05-24 17:10:24

0

解決您的問題,最好的辦法是要麼this.nodeName.toLowerCase()this.tagName.toLowerCase()更換$(this).attr("tag")

兩者都產生完全相同的結果!

0

而不是簡單地做:

$(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 
     // change the below line... 
     $(this).attr("id", "rnd" + this.tagName.toLowerCase() + "_" + i.toString()); 
    }); 
});