Home >Web Front-end >Front-end Q&A >Can jquery determine whether img is empty?

Can jquery determine whether img is empty?

青灯夜游
青灯夜游Original
2022-03-24 19:35:132438browse

jquery can determine whether img is empty. The value of the img tag is controlled by the src attribute; you only need to use the "$("img").attr("src")== ''" statement to determine whether the value of the img src attribute is equal to the empty string to determine whether the img It is empty, if it is equal to, it is empty, otherwise it is not empty.

Can jquery determine whether img is empty?

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery can determine whether img is empty.

  • The img tag is used to embed an image into a web page, and its value is controlled by the src attribute. (The src attribute specifies the URL where the image will be displayed).

  • You only need to determine whether the src attribute has a value.

Judgment method:

  • Use the attr() method to obtain the src attribute value of the image

  • Judge whether the attribute value is an empty string

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(function() {
				$("button").click(function() {
					var img = $("img").attr("src");
					if ($("img").attr("src") == &#39;&#39;) {
						alert("img为空");
					} else {
						alert("img不为空");
					}
				})
			})
		</script>
		<style>
			img{
				width: 200px;
				border:1px solid #FF0000;
			}
		</style>
	</head>
	<body>
		<img  src="img/1.jpg"/ alt="Can jquery determine whether img is empty?" ><br>
		<button>img是否为空</button>
	</body>
</html>

Can jquery determine whether img is empty?

If the src attribute has no value:

Can jquery determine whether img is empty?

[Recommended learning: jQuery video tutorial, web front-end development]

The above is the detailed content of Can jquery determine whether img is empty?. 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