Home >Web Front-end >Front-end Q&A >What is the code to delete horizontal lines in javascript

What is the code to delete horizontal lines in javascript

青灯夜游
青灯夜游Original
2022-03-28 18:30:162596browse

The code for javascript to delete horizontal lines is "body object.removeChild(hr object)". The horizontal line is defined using the hr element. You only need to use the javascript removeChild() function to remove the hr element from the parent element body to delete the horizontal line.

What is the code to delete horizontal lines in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript delete horizontal line

The horizontal line is defined using the hr element.

If you want to delete the horizontal line, you only need to use the javascript removeChild() function to delete the hr element from the parent element body to delete the horizontal line.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h1>这是一个大标题</h1>
		<p id="p1">这是一段文字。</p>
		<p id="p2">这是另一段文字。</p><hr id="hr" />
		<button onclick="myFunction()">删除水平线(hr)</button>
		<script>
			function myFunction() {
				var body = document.getElementsByTagName("body")[0];  //获取body元素的引用
				var hr = document.getElementById("hr");  //获取hr元素的引用
				body.removeChild(hr);
			}
		</script>
	</body>
</html>

What is the code to delete horizontal lines in javascript

Description:

removeChild() method can remove a node from the child node list. The usage is as follows:

nodeObject.removeChild(node)

The parameter node is the node to be deleted. If the deletion is successful, the deleted node is returned; if it fails, null is returned.

When using the removeChild() method to delete a node, all child nodes contained in the node will be deleted at the same time.

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What is the code to delete horizontal lines in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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