无法获得正确的 .replaceWith 方法选择器


这段

代码有一些问题,我想替换一段 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>

选择图像:

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

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

$('#background img').attr({
    'src': 'image source X',
    'alt': 'Background should be displayed here'
});

最新更新