Javascript method to add an image: First use the createElement() method to create an img element; then assign the URL address value of the image to the src attribute of the img element; finally use the appendChild() method to add the img element to the specified dom in the object.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to add pictures with javascript?
createElement() method
createElement() method creates an element by specifying a name
Syntax:
document.createElement(nodename)
Parameters: nodename: required. Create the name of the element.
Return value: Created element node
getElementById() method
getElementById() method can return the first object with the specified ID citation.
HTML DOM defines multiple methods to find elements, in addition to getElementById(), there are also getElementsByName() and getElementsByTagName().
If there is no element with the specified ID, return null
If there are multiple elements with the specified ID, return undefined.
Syntax:
document.getElementById(elementID)
appendChild() method
The appendChild() method adds a new child node to the end of the node's child node list.
Tip: If newchild already exists in the document tree, it will be removed from the document tree and reinserted in its new position. If newchild is a DocumentFragment node, it will not be inserted directly, but its child nodes will be inserted in order at the end of the childNodes[] array of the current node.
You can use the appendChild() method to remove an element to another element.
Syntax:
node.appendChild(node)
Parameters: node: required. The node object you want to add.
Example: JavaScript to add images to div elements
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript"> window.onload = function() { var bigImg = document.createElement("img"); //创建一个img元素 bigImg.src = "https://www.html.cn/static/images/logo.png"; //给img元素的src属性赋值 //bigImg.width="320"; //320个像素 不用加px var myp = document.getElementById('myp'); //获得dom对象 myp.appendChild(bigImg); //为dom添加子元素img }; </script> </head> <body> <div id="myp" style="border:1px solid #eee;width:320px;height:100px;"></div> </body> </html>
[Recommended: javascript advanced tutorial]
The above is the detailed content of How to add pictures in javascript. For more information, please follow other related articles on the PHP Chinese website!

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

Key goals and advantages of HTML5 include: 1) Enhanced web semantic structure, 2) Improved multimedia support, and 3) Promoting cross-platform compatibility. These goals lead to better accessibility, richer user experience and more efficient development processes.

The goal of HTML5 is to simplify the development process, improve user experience, and ensure the dynamic and accessible network. 1) Simplify the development of multimedia content by natively supporting audio and video elements; 2) Introduce semantic elements such as, etc. to improve content structure and SEO friendliness; 3) Enhance offline functions through application cache; 4) Use elements to improve page interactivity; 5) Optimize mobile compatibility and support responsive design; 6) Improve form functions and simplify verification process; 7) Provide performance optimization tools such as async and defer attributes.

HTML5transformswebdevelopmentbyintroducingsemanticelements,multimediacapabilities,powerfulAPIs,andperformanceoptimizationtools.1)Semanticelementslike,,,andenhanceSEOandaccessibility.2)Multimediaelementsandallowdirectembeddingwithoutplugins,improvingu

TherealdifferencebetweenusinganIDversusaclassinCSSisthatIDsareuniqueandhavehigherspecificity,whileclassesarereusableandbetterforstylingmultipleelements.UseIDsforJavaScripthooksoruniqueelements,anduseclassesforstylingpurposes,especiallywhenapplyingsty


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
