Solved

jQuery: Replace all CSS Background Images

Posted on 2013-06-03
3
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

752 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