jQuery Dynamic Remove and Add Background Picture Method Detailed Explanation

  • 2021-07-26 06:39:36
  • OfStack

In this paper, an example is given to describe the method of dynamically removing and adding background pictures in jQuery. Share it for your reference, as follows:

Using jQuery to remove and add pictures

1. Style


<style type="text/css">
  .changeImage{
     background:url(images/right.png) no-repeat center;
  }
</style>

2. JS

(1) When changing the style of a label, you need to remove the style you added before


$("#tab tr").find("td").removeClass("changeImage");

(2) Add Styles


$("#tab tr").find("td").addClass("changeImage");

Attached: Complete example demo:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Remove and add pictures </title>
<script type="text/javascript" src="jquery-1.7.2.min.js" ></script>
<style type="text/css">
.changeImage{
 background:url(images/arrow1.gif) no-repeat center;
}
</style>
<script>
function removeimg(){
$("#tab tr").find("td").removeClass("changeImage");
}
function addimg(){
$("#tab tr").find("td").addClass("changeImage");
}
</script>
</head>
<body>
  <table border=1 id="tab">
  <tr><td> Year </td><td> Manufacturer </td><td> Model </td><td> Description </td><td> Value </td></tr>
  <tr><td>1997</td><td>Ford</td><td>E350</td><td>"ac</td><td> abs</td><td> moon"</td><td>3000.00</td></tr>
  <tr><td>1999</td><td>Chevy</td><td>"Venture ""Extended Edition"""</td><td>""</td><td>4900.00</td></tr>
  <tr><td>1999</td><td>Chevy</td><td>"Venture ""Extended Edition</td><td> Very Large"""</td><td>""</td><td>5000.00</td></tr>
  <tr><td>1996</td><td>Jeep</td><td>Grand Cherokee</td><td>"MUST SELL!</td></tr>
  <tr><td>air</td><td> moon roof</td><td> loaded"</td><td>4799.00</td></tr>
  </table>
  <input type="button" value=" Add Style " onclick="addimg()"/>
  <input type="button" value=" Delete Style " onclick="removeimg()"/>
</body>
</html>

More readers interested in jQuery can check the topics of this site: "jQuery Picture Operation Skills Encyclopedia", "jQuery Table (table) Operation Skills Summary", "jQuery Switching Special Effects and Skills Summary", "jQuery Extension Skills Summary", "jQuery Common Plug-ins and Usage Summary", "jQuery Common Classic Special Effects Summary" and "jquery Selector Usage Summary"

I hope this article is helpful to everyone's jQuery programming.


Related articles: