Solved

jQuery: Replace all CSS Background Images

Posted on 2013-06-03
3
316 Views
Last Modified: 2013-06-05
I use jQuery to replace all images that are added with the <img> tag.  How can I also replace all images that are added with CSS?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
  $('img').each(function () {
   if($(this).attr('src')) $(this).attr('src','http://upload.wikimedia.org/wikipedia/en/0/0f/Red_X_Symbol.gif');
  });
});

</script>
<style type="text/css">
h1 {
background: transparent url(http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png) no-repeat scroll 0 50%;
padding: 0 0 0 20px;
}
</style>

</head>
<body>

<h1>Logo</h1>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png" alt="Smile" /></p>
<p style="padding: 0; margin: 0; width: 16px; height: 16px; background: transparent url(http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png) repeat scroll 0 0;"></p>
<div><img src="http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png" alt="Smile" /></div>
<p style="padding: 0; margin: 0; width: 16px; height: 16px; background: transparent url(http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png) repeat scroll 0 0;"></p>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39217701
Assume you mean has a background-image?

if ($('element').css('background-image')){
...
}
0
 
LVL 16

Author Comment

by:hankknight
ID: 39224100
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

  $('img').each(function () {
   if($(this).attr('src')) $(this).attr('src',$(this).attr('src')+'?refresh');
   if($(this).attr('longdesc')) $(this).attr('longdesc',$(this).attr('longdesc')+'?refresh');
  });
  $('*').each(function () {
   if($(this).css('background-image') && $(this).css('background-image')!='none') {
       $(this).css('backgroundImage', 'url("'+ $(this).css('background-image').replace(/^url\(['"](.+)['"]\)/, '$1')+'?refresh' +'")');
   }
  });

});

</script>
<style type="text/css">
h1 {
background: transparent url(http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png) no-repeat scroll 0 50%;
padding: 0 0 0 20px;
}
</style>

</head>
<body>

<h1>Logo</h1>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png" alt="Smile" /></p>
<p style="padding: 0; margin: 0; width: 16px; height: 16px; background: transparent url(http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png) repeat scroll 0 0;"></p>
<div><img src="http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png" alt="Smile" /></div>
<p style="padding: 0; margin: 0; width: 16px; height: 16px; background: transparent url('http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png') repeat scroll 0 0;"></p>
<p style="padding: 0; margin: 0; width: 16px; height: 16px; background: transparent url('http://upload.wikimedia.org/wikipedia/commons/8/85/Emoticon_smile.png") repeat scroll 0 0;'></p>
</body>
</html>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39224113
Looping each and every element on the page with * is probably not a good idea - if you can, narrow it down to div, span etc.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question