DOM - Método de objeto de elemento - removeAttributeNS

O método removeAttributeNS remove um atributo por nome local e URI de namespace.

Sintaxe

A seguir está a sintaxe para o uso do método removeAttributeNS .

elementObj.removeAttributeNS(namespace,attrName)

S.No. Parâmetro e Descrição
1

namespace

É uma string que especifica o namespace do atributo.

2

attrName

É uma string que nomeia o atributo a ser removido do nó atual.

Ele retorna um nó Attr para o atributo especificado.

Exemplo

O conteúdo do node_ns.xml é o seguinte -

<?xml version = "1.0"?>
<Company>
   <Employee xmlns:e = "http://www.tutorials.com/technical/" category = "technical">
      <e:FirstName e:lang = "en">Tanmay</e:FirstName>
      <e:LastName>Patil</e:LastName>
      <e:ContactNo>1234567890</e:ContactNo>
      <e:Email>[email protected]</e:Email>
   </Employee>
   
   <Employee xmlns:n = "http://www.tutorials.com/non-technical/" category = "non-technical">
      <n:FirstName n:lang = "en">Taniya</n:FirstName>
      <n:LastName>Mishra</n:LastName>
      <n:ContactNo>1234667898</n:ContactNo>
      <n:Email>[email protected]</n:Email>
   </Employee>
</Company>

O exemplo a seguir demonstra o uso do método removeAttributeNS -

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node_ns.xml");
         x = xmlDoc.getElementsByTagName("FirstName")[0];
         ns = "http://www.tutorials.com/technical/";
         document.write("Before removing the attributeNS: ");
         document.write(x.getAttributeNS(ns,"lang"));
         x.removeAttributeNS(ns,"lang");
         document.write("<br>After removing the attributeNS: ");
         document.write(x.getAttributeNS(ns,"lang"));
      </script>
   </body>
</html>

Execução

Salve este arquivo como elementattribute_removeAttributeNS.htm no caminho do servidor (este arquivo e node_ns.xml devem estar no mesmo caminho em seu servidor). Obteremos a saída conforme mostrado abaixo -

Before removing the attributeNS: en
After removing the attributeNS: null