Solved

jQuery: Replace all CSS Background Images

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now