


JavaScript three methods to obtain, set and remove element attributes_javascript skills
Take the following html as an example
1. Obtain and set element properties through the attributes of HTMLElement type (object)
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//Get element properties
alert(div.id); //"myDiv"
alert(div.className); //"bd", this is not div.class, because class is a reserved keyword
alert(div.title); //"I am div"
alert(a. href); //http://www.baidu.com
//Set element properties
div.id = "myDiv2"; //Change id to "myDiv2"
div.className = "ft "; //class is changed to "ft". If there is a style named "ft", it will immediately change to the "ft" style, and the browser will respond immediately
div.title = "I am myDiv2"; / /title is changed to "I am myDiv2"
div.align = "center"; //Set center alignment
img.src ="images/img1.gif"; //Set image path
a. innerHTML = "Sina"; //"Baidu" is changed to "Sina"
a.href = "http://www.sina.com.cn"; //Reset the hyperlink
2. Get, set, and remove the characteristics of elements through the getAttribute(), setAttribute(), and removeAttribute() methods (not recommended, the first two methods have exceptions in IE6 and 7, and the third method is not supported by IE6 , can be used when setting custom attributes)
getAttribute() method is used to obtain element attributes. It accepts one parameter, which is to get the attribute name of the element. The
setAttribute() method is used to set the attribute of the element. It accepts two parameters, namely to obtain the attribute name and attribute value of the element. The
removeAttribute() method is used to remove the attribute of the element. Accepts one parameter, which is the attribute name of the element to be removed
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//Get element properties
alert(div.getAttribute("id")); //"myDiv"
alert(div.getAttribute("class")); //"bd", note that this is class, not className, Different from the above
alert(div.getAttribute("title")); //"I am div"
alert(a.getAttribute("href")); //http://www.baidu. com
//Set element attributes
div.setAttribute("id","myDiv2"); //Change id to "myDiv2"
div.setAttribute("class","ft"); / /class is changed to "ft", here it is also class, not className
div.setAttribute("title","I am myDiv2"); //title is changed to "I am myDiv2"
div.setAttribute ("align","center"); //Set center alignment
img.setAttribute("src","images/img1.gif"); //Set image path
//Remove element attributes
div.removeAttribute("class"); //Remove the class attribute
3. Get, set, and remove the attributes of the element through the attributes attribute
var div = document.getElementById("myDiv");
//Get element properties
alert(div.attributes["id"].nodeValue); //"myDiv"
//Set element attributes
div.attributes["id"].nodeValue = "myDiv2"; //id Change to "myDiv2"
//Remove element attributes
div.attributes.removeNamedItem("class"); //Remove class attributes

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Everyone can get a lot of information on the Xiaohongshu APP. There are many functions and services here, all of which can be operated by users freely. According to their own needs, they can choose some corresponding functions and operations here to solve the problem. Some of your questions are particularly convenient. I can really recommend a large number of these notes to you every day. They are rich in content and cover a wide range. You can choose freely, no matter which content section you want to see here. We can satisfy everyone here and solve some of your problems. When you are free, you can try to post various notes by yourself. Maybe everyone will have the opportunity to gain a large number of fans, so you don’t want to lose some If fans pay attention, they can choose to remove this

1. Open my page in Weibo and click the gear settings icon in the upper right corner. 2. After entering the settings page, click the Account and Security option. 3. On the Account and Security page, click the option of recent login records. 4. After entering the page of recent login records, click Exit behind the device you want to log out. 5. Then click the Confirm button in the pop-up window.

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Using jQuery to remove the z-index attribute is a very simple operation. The following will demonstrate how to achieve this operation through specific code examples. First, we need to introduce the jQuery library into HTML, you can use the following CDN link: &

We are provided an array and our task is to remove leading zeros from the given array and then print the array. Input:arr[]={0,0,0,1,2,3}Output:123Input:arr[]={0,0,0,1,0,2,3}Output:1023We can create a new An array that does not contain leading zeros from the previous array in the given problem. Method to find the solution In this method we will iterate through the array and insert all the numbers except leading zeros. Example #include<iostream>usingnamespacestd;intmain(){ &a

Discuss a problem given a binary number. We have to remove a little bit from it so that the remaining number should be the maximum among all other options like Input:N=1011Output:111Explanation:Weneedtoremoveonebitsoremoving0bitwillgiveamaximumnumberthanremovingany1’sbit.111>101,011.Input:111Output:11Explanation:Sinceallthebitsare1sowecanremovean

Discuz bottom information removal tutorial With the continuous development and popularity of the Discuz forum system, many webmasters and administrators want to personalize the website when building their own forums, including removing the bottom information. In this article, we will share with you a specific tutorial on how to remove the information at the bottom of Discuz, and provide code examples to help you complete the operation easily. Step 1: Log in to the Discuz backend. First, you need to log in to the backend management system of the Discuz forum and enter the correct username and password.

jQuery Animation Tips: How to Remove Page Elements Elegantly In web development, we often encounter situations where page elements need to be added or removed dynamically. When removing page elements, we often hope to display them with an elegant animation effect to make the user experience smoother and more comfortable. In this article, we will explore how to use jQuery to elegantly remove page elements and provide specific code examples for reference. 1. Use the .fadeOut() method. The fadeOut() method can make the element fade
