对这段代码有一些问题,我想替换那些定义背景是什么的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>
答案 0 :(得分:3)
选择图片:
$('#background img').replaceWith(' ... ');
不要替换元素,只需更新其属性:
$('#background img').attr({
'src': 'image source X',
'alt': 'Background should be displayed here'
});