Solved

jQuery: Replace all CSS Background Images

Posted on 2013-06-03
3
313 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now