Solved

jQuery: Replace all CSS Background Images

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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 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)

623 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