Home > Web Front-end > JS Tutorial > body text

Detailed explanation of usage examples of javascript show/hide, create/delete html elements

伊谢尔伦
Release: 2017-07-18 15:32:40
Original
1859 people have browsed it

Show/Hide

1. Write js function

Copy after login

2. Add the id attribute to the html element to be displayed/hidden

Copy after login

3, add buttons, links, etc. to trigger the js function

 
显示/隐藏 
javascript显示隐藏层 
Copy after login

2: Two methods for javascript to control the hiding and display of page controls
Two methods for javascript to control the hiding and display of page controls. The difference between the methods is whether the control still occupies space on the page after it is hidden
Method one:

document.all["PanelSMS"].style.visibility="hidden"; 
document.all["PanelSMS"].style.visibility="visible";
Copy after login

Method two:

document.all["PanelSMS"].style.display="none"; 
document.all["PanelSMS"].style.display="inline";
Copy after login

After method one is hidden, the position of the page is still occupied by the control but it is not displayed
Method two is after hiding the page The position is not occupied

Create/Delete

If I want to create a div element.

1. Create using DOM objects:

Use document.createElement('div') method to create elements.

2. Use JQuery to create:

Use the $('

New element created by JQuery
') method to create elements directly.

If you need to delete the div element with the ID 'div2js'.

1. Using the DOM object

First you need to find the parent element of the deleted element, and delete the child elements that need to be deleted through the parent element.

var el = document.getElementById('div2js');
 el.parentNode.removeChild(el);
Copy after login

2. Use JQuery

to find and delete directly.

$('#div2js').remove();
Copy after login

Finally let’s look at an example


姓名:
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!