Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [DOM] esiste un metodo per verificare il tag di un nodo senza l'uso...?

    Salve gente,
    volevo sapere se esisteva un metodo per ricavare da un nodo il tipo di tag(attenzione non l'attributo name ma il tipo di tag es. "p")

    Es.

    var nodo = document.body.firstChild;

    if("tag di nodo" == "p")

    fai questo;

    else

    fai quest'altro;


    questo senza l'uso di getElementsByTagName che in ogni caso non mi è utile perchè mi restituisce l'array di tutti gli elementi del documento.

    Grazie mille.

  2. #2
    mi basterebbe sapere anche che non esiste un modo.

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Dovrebbe essere tagName

    codice:
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    
    function enumTags() {
      allTags = document.getElementsByTagName('body')[0].getElementsByTagName("*");
      for (i = 0; i < allTags.length; i++) {
        alert("nome tag rilevato: "+allTags[i].tagName);
      }
    }
    
    </script>
    </head>
    <body>
    <h1>Un tag Title 1</h1>
    
    
    Un paragrfo in stile normale</p>
    <h2>Un tag Title 2</h1>
    Un link
    
    Un testo in cicciotto
    <button name="button" onclick="enumTags();">Il bottone di verifica</button>
    </body>
    </html>
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    ti meriterebbe usare nodeName

    var foo=document.getElementById("echeneso");
    alert(foo.nodeName)

  5. #5
    Oh, occhio: nodeName ritorna il tag uppercased. I tuoi confronti debbono essere fatti non come
    =="p"
    ma=="P"

    oppure mettendo tutto lowercase o uppercase toUpperCase() etc...

  6. #6
    Oppure con una regex case insensitive /p/i.test(foo.tagName)
    sprecata però in questo caso.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.