Home Web Front-end JS Tutorial The use of dynamically creating and deleting elements in DOM-based programming_Basic knowledge

The use of dynamically creating and deleting elements in DOM-based programming_Basic knowledge

May 16, 2016 pm 05:36 PM
dom Delete element

Copy code The code is as follows:



< ;script type="text/javascript">
function test(){
//createElement() Create an element with a specified tag name [for example: dynamically create a hyperlink]
var createa=document. createElement("a");
createa.id="a1";
createa.innerText="Connect to Baidu";
createa.href="http://www.baidu.com";
//createa.color="green" ////Add color (don't forget the style attribute, otherwise it will have no effect)
createa.style.color="green"
//Add default location-- body and add child nodes
//document.body.appendChild(createa);
//Place the specified position
>
function test2(){
//Delete node at specified position removeChild()
document.getElementById("div1").removeChild(document.getElementById("a1")); //Duplicate id name js only takes the first
}


















< input type="button" value="Create an a tag" onclick="test()"/>








Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete elements but keep child elements in jquery How to delete elements but keep child elements in jquery Nov 19, 2021 pm 02:22 PM

How to delete elements but keep child elements in jquery: 1. Use the children() method to get all the child elements of the specified element; 2. Use the unwrap() method to delete the parent element of the child element but keep the child elements. The syntax "$(" specifies the element. ").children().unwrap();".

What are the ways to obtain DOM nodes in Vue3 What are the ways to obtain DOM nodes in Vue3 May 11, 2023 pm 04:55 PM

1. Native js gets the DOM node: document.querySelector (selector) document.getElementById (id selector) document.getElementsByClassName (class selector).... 2. Get the instance object of the current component in vue2: because each vue Each component instance contains a $refs object, which stores references to the corresponding DOM elements or components. So by default, the component's $refs point to an empty object. You can first add ref="name" to the component, and then pass this.$refs.

DOM manipulation guide in PHP DOM manipulation guide in PHP May 21, 2023 pm 04:01 PM

In web development, DOM (DocumentObjectModel) is a very important concept. It allows developers to easily modify and operate the HTML or XML document of a web page, such as adding, deleting, modifying elements, etc. The built-in DOM operation library in PHP also provides developers with rich functions. This article will introduce the DOM operation guide in PHP, hoping to help everyone. The basic concept of DOM DOM is a cross-platform, language-independent API that can

How to delete elements from set in javascript How to delete elements from set in javascript Jan 12, 2022 am 10:56 AM

Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete all elements in the Set object, the syntax is "setObj.delete(value);" "setObj.clear();".

What does vue dom mean? What does vue dom mean? Dec 20, 2022 pm 08:41 PM

DOM is a document object model and an interface for HTML programming. Elements in the page are manipulated through DOM. The DOM is an in-memory object representation of an HTML document, and it provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

What is the reason why ref binding to dom or component fails in vue3 and how to solve it What is the reason why ref binding to dom or component fails in vue3 and how to solve it May 12, 2023 pm 01:28 PM

Vue3ref binding DOM or component failure reason analysis scenario description In Vue3, it is often used to use ref to bind components or DOM elements. Many times, ref is clearly used to bind related components, but ref binding often fails. Examples of ref binding failure situations The vast majority of cases where ref binding fails is that when the ref is bound to the component, the component has not yet been rendered, so the binding fails. Or the component is not rendered at the beginning and the ref is not bound. When the component starts to render, the ref also starts to be bound, but the binding between ref and the component is not completed. At this time, problems will occur when using component-related methods. The component bound to ref uses v-if, or its parent component uses v-if to cause the page to

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

How to delete an element with a specified key in a PHP array How to delete an element with a specified key in a PHP array Jul 07, 2023 pm 11:15 PM

How to delete an element with a specified key name in a PHP array In PHP development, array is a very important data structure. When operating an array, you often encounter situations where you need to delete elements with specified key names. This article will use code examples to introduce how to delete elements with specified key names in PHP arrays. PHP provides a variety of methods to delete elements in an array. Three of the commonly used methods are introduced below. Method 1: Use the unset() function The unset() function can be used to destroy specified variables, including elements in the array.

See all articles