有没有办法更改标签内的src-url?

问题描述 投票:-1回答:1

我有一个上传部分,我想上传图像。但我不想使用标准的上传按钮,因此我有一个带有图像的标签。

HTML

  <div class="image-upload">
    <label for="imgInp"><img id="blah" src="https://www.gravatar.com/avatar/9a6676e137fefad07752853bcc656a39?s=48&d=identicon&r=PG&f=1" width="320px" height="240" style="padding-bottom:10px" /></label>
    <input type='file' id="imgInp" />
  </div>

脚本

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});

css隐藏默认上传按钮

.image-upload > input {
  display: none;
}

我想要做的是更改标签标签内的图像src-url。什么是实现这一目标的聪明方法?我尝试了类似的东西,但当我用标签封装图像预览时,代码不再起作用了。

javascript
1个回答
0
投票

这只是一个小错字。你用过了

$('#blah').attr('src', e.target.result);

但你的图像元素的ID是啰嗦

© www.soinside.com 2019 - 2024. All rights reserved.