用单选按钮更改背景图像

sha*_*baz 5 css jquery

我想用单选按钮选择更改html的backgroundImage.每个单选按钮都有不同的图像.我试过它:

      <input name="BG" id="wood" type="radio" value="" class="bgCollection" />
      <label for="radio">Wood</label>

      <input name="BG" id="steel" type="radio"  class="bgCollection"/>
      <label for="radio">Steel</label>

      <input name="BG" id="metal" type="radio" value="" class="bgCollection"/>
      <label for="radio">Black metal</label>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

    $(document).ready(function(){
            $(".bgCollection").change(
               function()
          {
        if($("input#wood").attr("checked"))  
                  {$("html").css('backgroundImage','url(../images/wood.jpg)');}
        if($("input#steel").attr("checked")
                   {$("html").css('backgroundImage','url(../images/BG-steel.jpg)');}
        if($("input#metal").attr("checked"))
                   {$("html").css('backgroundImage','url(images/blackMetal.jpg)');}
       });
  });
Run Code Online (Sandbox Code Playgroud)

但这样背景并没有改变.

gau*_*171 2

这里我针对上述问题做了完整的demo。请检查演示链接。

演示: http: //codebins.com/bin/4ldqp80

HTML:

<input name="BG" id="wood" type="radio" value="" class="bgCollection" />
<label for="radio">
  Wood
</label>

<input name="BG" id="steel" type="radio"  class="bgCollection"/>
<label for="radio">
  Steel
</label>

<input name="BG" id="metal" type="radio" value="" class="bgCollection"/>
<label for="radio">
  Black metal
</label>
Run Code Online (Sandbox Code Playgroud)

jQuery:

$(function() {
    $(".bgCollection").change(function() {
        if ($("#wood").is(":checked")) {
            $("body").css('backgroundImage', "url('http://cdnimg.visualizeus.com/thumbs/7f/78/gfx,wood-7f78592c9a6ed3390492c560c5ac6fec_h.jpg')");
        }
        if ($("#steel").is(":checked")) {
            $("body").css('backgroundImage', "url('http://www.aroorsteelcorporation.com/full-images/stainless-steel-834007.jpg')");
        }
        if ($("#metal").is(":checked")) {
            $("body").css('backgroundImage', "url('http://i00.i.aliimg.com/photo/v0/468538622/Brushed_metal_texture_prepainted_metal.jpg')");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

演示: http: //codebins.com/bin/4ldqp80