【发布时间】:2023-03-25 16:52:02
【问题描述】:
以下是设置背景图片和背景图片的html-javascript代码。
<html>
<link rel="stylesheet" type="text/css" href="try2.css">
<body>
Choose the color<br>
<div class="foo" id="#13b4ff" style="background-color:#13b4ff;"></div>
<div class="foo" id="ab3fdd" style="background-color:#ab3fdd;"></div>
<div class="foo" id="ae163e" style="background-color:#ae163e;"></div>
<br><br>
<div id="myframe1" style="padding:5px;width:300px;height:400px;border:1px solid black;">
<p><img src="img-thing.png" style="width:200px;height:200px;"/><p>
</div>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$('.foo').click(function(){
var str1 = $(this).attr('id');
var myframe = document.getElementById('myframe1');
myframe.style.backgroundColor=str1;
myframe.style.width=300;
myframe.style.height=400;
});
});
</script>
<div><input type='file' onchange="readURL(this);" />
<img id="blah"/></div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#myframe1').css({
'background':'url('+e.target.result +')',
'background-size':'310px 410px'
});
};
reader.readAsDataURL(input.files[0]);//To display images uncomment this
}
}
</script>
</body>
</html>
颜色的 CSS 文件是(以防万一您也需要查看)
.foo {
float: left;
width: 20px;
height: 20px;
margin: 5px 5px 5px 5px;
border-width: 1px;
border-style: solid;
border-color: rgba(0,0,0,.2);
}
现在的问题是: 我希望该用户可以先单击上传图片选项并将图片上传为背景。但是一旦完成,它就不允许用户将颜色作为背景。如何解决?相反,如果选择颜色然后选择图像,图像可以覆盖为背景。我希望两者都必须能够相互覆盖。为方便起见,我还提供了小提琴链接:here 小提琴中还有一个问题,其他颜色没有显示,但它们在我的 html 文件中工作。
【问题讨论】:
-
你能试试把
'background':'url('+e.target.result +')'改成'background-image':'url('+e.target.result +')'吗?
标签: javascript jquery html css image