简体   繁体   English

使用JQuery属性和HTML输入更改图像src

[英]Change image src using JQuery Attributes and an HTML input

Good evening. 晚上好。

I am looking for a bit of help in a Code I found around, but I can't get towork as I would like: 我在周围找到的代码中寻求帮助,但由于需要,我无法上班:

This code, essentially, let's you insert in the Input the URL of an image (Let's say " http://i.blogs.es/e79516/nuevo-logo-google/650_1200.jpg ") and once you left the input, the script automatically shows it up in the image below. 从本质上讲,这段代码让您在“输入”中插入图片的网址(例如,“ http://i.blogs.es/e79516/nuevo-logo-google/650_1200.jpg ”),然后离开输入,该脚本会自动在下图中显示它。

The problem, is that I would like to enter only the name of the archive, not the entire URL , but I don't know how or where to enter all the URL route and left only the dynamic part to change, since I am beginning with Js. 问题是,我只想输入档案名称,而不是整个URL ,但是我不知道如何或在哪里输入所有URL路由,而只剩下动态部分进行更改,因为我开始与Js。

So, imagining I want to show only images already created and ubicated in "//localhost/ROL/images/Avatars/Characters/ "NAME OF THE ARCHIVE" .png", Which changes should I make to only enter the file name, without even the extension? 因此,假设我只想显示已在“ // localhost / ROL / images / Avatars / Characters / “ NAME OF THE ARCHIVE” .png“中创建并图像化的图像,那么我应该进行哪些更改以仅输入文件名,而无需输入甚至扩展名?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery-ui.css">
</head>
<body>
<tr>
        <td valign="top">
            Profile Picture:<br>
            <span class="small3">(will be reduced to 50x50)</span>
        </td>
        <td>
            <input size="100" id="newProfilePicture" name="profpic" value="/%profpic%/"> 
            <br>
            <img id="profilePicture" src="/%profimg%/">
        </td>
    </tr>
</body>
<script>
$('#newProfilePicture').blur(function() {
        var newSrc = $('#newProfilePicture').val();
        $('#profilePicture').attr('src',newSrc);
    });
</script>
</html>

Thank you for the Attention 谢谢您的关注

.blur(function(){
    $('#profilePicture').prop('src',"i.blogs.es/e79516/nuevo-logo-google/"+$('#newProfilePicture').val()+".png");
});

Works but not necessary to save the value to a variable. 有效,但不必将值保存到变量。 You also may want to use prop() instead of attr. 您可能还想使用prop()而不是attr。 (Sorry I missed your OP question asking to tack on the ".png") (对不起,我错过了您提出要求添加“ .png”的问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM