Home  >  Article  >  Web Front-end  >  How to remove the background of div with jquery

How to remove the background of div with jquery

青灯夜游
青灯夜游Original
2022-05-24 14:58:342045browse

Two methods: 1. Use css() to set the background attribute, the syntax is "$("div").css('background','none')"; 2. Use attr() to add a new background Style, syntax "$("div").attr('style','background:none')".

How to remove the background of div with jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

Two ways to remove div background with jquery:

##Method 1. Use css()

Use css() to set the background attribute of the div element, the value is none

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-3.2.1.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("div").css(&#39;background&#39;,&#39;none&#39;);
				});
			});
		</script>
		<style>
			div{
				height: 100px;
				margin: 10px;
				background:url(img/2.png)
			}
		</style>
	</head>
	<body>

		<button>去掉div的背景</button>
		<div>hello</div>

	</body>
</html>

How to remove the background of div with jquery

##2. Use attr()


Use attr() to set the style attribute for the div element and add the background:noney style.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-3.2.1.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("div").attr(&#39;style&#39;,&#39;background:none&#39;);
				});
			});
		</script>
		<style>
			div{
				height: 100px;
				margin: 10px;
				background:red;
			}
		</style>
	</head>
	<body>

		<button>去掉div的背景</button>
		<div>hello</div>

	</body>
</html>

How to remove the background of div with jquery【Recommended learning:

jQuery video tutorial

, web front-end video

The above is the detailed content of How to remove the background of div with jquery. 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