0

这段代码有一些问题,我想替换一段 html 代码,该代码在从单选按钮选项中选择一个选项时定义背景是什么。我似乎无法正确选择选择器。

<html>
<head>
    <title>Javascript</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script type="text/javascript">

        $(document).ready(function(){
            $('#getValue').click(function(){
                var input = $('input[name=choice]:checked').val();
                });
            if(input = 1)
            {
                $('#background').replaceWith('<img src="image source 1" alt="Background should be displayed here">');
            }
            if(input = 2)
            {
                $('#background').replaceWith('<img src="image source 2" alt="Background should be displayed here">');
            }
            if(input = 3)
            {
                $('#background').replaceWith('<img src="image source 3" alt="Background should be displayed here">');
            }
        });
    </script>
</head>

<body id="body">

<div id="background">
        <img src="image source 1" alt="Background should be displayed here">
</div>

<div id="footer">
        <input type="radio" name="choice" value="1" />Scheme A
        <input type="radio" name="choice" value="2" />Scheme B
        <input type="radio" name="choice" value="3" />Scheme C
        <input type="button" id="getValue" value="SELECT SCHEME"/>
</div>

</body>

</html>
4

1 回答 1

3

选择图片:

$('#background img').replaceWith(' ... ');

无需替换元素,只需更新其属性:

$('#background img').attr({
    'src': 'image source X',
    'alt': 'Background should be displayed here'
});
于 2012-04-29T16:26:32.300 回答