Home > Article > Web Front-end > js changes html
With the continuous development of the Internet, the interactivity of web pages is becoming more and more important, and JavaScript, as a scripting language, can well realize the interaction of web pages. Among them, modifying HTML elements is one of the important functions in JavaScript, which allows web pages to achieve richer interactive effects.
JavaScript method of modifying HTML elements
var element = document.getElementById("elementID");
Among them, elementID is the ID of the element that needs to be modified.
var elements = document.getElementsByClassName("className");
Among them, className is the class name of the element that needs to be modified, and an array is returned.
var elements = document.getElementsByTagName("tagName");
Among them, tagName is the tag name of the element that needs to be modified, and an array is returned.
var element = document.querySelector(".example");
Among them, ".example" is the CSS class selector of the element that needs to be obtained, and only the first matching element is returned.
var elements = document.querySelectorAll(".example");
Among them, ".example" is the CSS class selector of the element that needs to be obtained, and an array is returned.
JavaScript to modify the attributes and content of HTML elements
After obtaining the element that needs to be modified, you can achieve the modification effect by setting its attributes or content. The specific method is as follows:
element.attribute = value;
Among them, attribute is the name of the attribute that needs to be modified, and value is the new value of the attribute. For example, to modify the src attribute of an image to a new link, you can write:
var img = document.getElementById("myImg"); img.src = "newImg.jpg";
element.innerHTML = newContent;
Among them, newContent requires new content. For example, to modify the text content of a paragraph, you can write like this:
var p = document.querySelector(".example"); p.innerHTML = "这是一个新的文本内容";
JavaScript to modify the style of HTML elements
In addition to modifying the attributes and content of HTML elements, JavaScript can also modify the style of elements. Give the web page a more beautiful appearance. The specific usage is as follows:
element.style.property = value;
Among them, property is the name of the CSS style attribute that needs to be modified, and value is the new value of the attribute. For example, to modify the background color of an element, you can write:
var element = document.getElementById("myDiv"); element.style.backgroundColor = "red";
JavaScript to modify the class of HTML elements
JavaScript can also modify the class of HTML elements to achieve richer style effects. The usage is as follows:
element.className = "newClassName";
Among them, newClassName is the new class name that needs to be assigned to the element. For example, adding a new class to an element can be written like this:
var element = document.querySelector(".example"); element.className = "newClass";
Summary
JavaScript modifying HTML elements is an important method to achieve web page interaction. By obtaining the elements that need to be modified, you can achieve Modification of element attributes, content, styles and classes. Mastering these methods can allow us to achieve richer interactive effects during web development.
The above is the detailed content of js changes html. For more information, please follow other related articles on the PHP Chinese website!