我如何使用输入标签结果到 img.src

米塞尔·因德拉·维贾亚

所以我想使用输入结果作为图像源我该怎么做?

我尝试使用 input id asimg.src但这显然不起作用,我也尝试使用imgtag 并使用 put input result 作为imgtag src,然后调用它canvas,但也没有用,我真的是编码新手,所以请如果我的问题看起来很愚蠢,我深表歉意:

html

<canvas id="image"></canvas>

js

var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function(){
canvas.width = img.naturalWidth
canvas.height = img.naturalHeight
ctx.drawImage(img, 0, 0);
}
img.src =
学习者

在某个动作上,只需获取输入值并将其放入图像 src 属性中..我想应该可以吗?我在这里使用了一个按钮作为操作:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <input type="text" name="image_src" id="image-src">
    <img id="input-image">
    <button onClick="putInputValue()">Use</button>
    <script>
        function putInputValue() {
            var imageSrcValue = document.getElementById("image-src").value;
            var image = document.getElementById("input-image");
            image.setAttribute('src', imageSrcValue);
        }
    </script>
</body>
</html>

您对此答案的评论需要一个全新的实现,而不是您在问题中提出的实现。

如果您只想呈现输入 url 并将其显示为图像,您应该使用以下内容:https : //stackoverflow.com/a/14053147/6935763

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章