Propiedad HTML DOM interiorHTML
Ejemplo
Cambie el contenido HTML de un elemento <p> con id="demo":
document.getElementById("demo").innerHTML = "Paragraph changed!";
Más ejemplos de "Pruébelo usted mismo" a continuación.
Definición y uso
La propiedad innerHTML establece o devuelve el contenido HTML (HTML interno) de un elemento.
Compatibilidad con navegador
Property | |||||
---|---|---|---|---|---|
innerHTML | Yes | Yes | Yes | Yes | Yes |
Sintaxis
Devuelve la propiedad innerHTML:
HTMLElementObject.innerHTML
Establezca la propiedad innerHTML:
HTMLElementObject.innerHTML = text
Valores de propiedad
Value | Description |
---|---|
text | Specifies the HTML content of an element |
Detalles técnicos
Valor de retorno: | Una cadena, que representa el contenido HTML de un elemento |
---|
Más ejemplos
Ejemplo
Obtenga el contenido HTML de un elemento <p> con id="myP":
var x = document.getElementById("myP").innerHTML;
Ejemplo
Obtenga el contenido HTML de un elemento <ul> con id="myList":
var x = document.getElementById("myList").innerHTML;
Ejemplo
Cambia el contenido HTML de dos elementos:
document.getElementById("myP").innerHTML = "Hello Dolly.";
document.getElementById("myDIV").innerHTML = "How are you?";
Ejemplo
Alerte el contenido HTML de un elemento <p> con id="demo":
alert(document.getElementById("demo").innerHTML);
Ejemplo
Elimine el contenido HTML de un elemento <p> con id="demo":
document.getElementById("demo").innerHTML = ""; // Replaces the content of <p> with an empty string
Ejemplo
Cambie el contenido HTML, la URL y el destino de un enlace:
document.getElementById("myAnchor").innerHTML = "W3Schools";
document.getElementById("myAnchor").href = "https://www.w3schools.com";
document.getElementById("myAnchor").target = "_blank";
❮ Objeto Elemento