Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在数组中存储图像(javascript)?_Javascript_Html_Arrays - Fatal编程技术网

如何在数组中存储图像(javascript)?

如何在数组中存储图像(javascript)?,javascript,html,arrays,Javascript,Html,Arrays,我正在尝试使用javascript将图像存储在数组中。我还想将该数组中的每个图像指定给下拉列表中的一个选项。这些映像位于不同的服务器上 无论我尝试什么,我的函数都不起作用。有人能帮我吗 另外,在这种情况下,我只允许使用javascript(家庭作业:/),我必须使用数组来存储图像 HTML代码: <form action="#" method="get" accept charset="utf-8"> <select id="car"> <option>

我正在尝试使用javascript将图像存储在数组中。我还想将该数组中的每个图像指定给下拉列表中的一个选项。这些映像位于不同的服务器上

无论我尝试什么,我的函数都不起作用。有人能帮我吗

另外,在这种情况下,我只允许使用javascript(家庭作业:/),我必须使用数组来存储图像

HTML代码:

<form action="#" method="get" accept charset="utf-8"> 

<select id="car">

<option> BMW </option>
<option> Mercedes </option>
<option> Audi </option>
<option> VW </option>

</select>

<input type="button" value="select" onclick="myFunction();">

</form>
}


谢谢你的回答

该代码应该可以工作:

function myFunction()
{
    var pictures = [
        "https://pixabay.com/p-848904/?no_redirect",
        "http://www.pruebas.pieldetoro.net/web/MERCEDES/DOSSIERES/w115/8-1.jpg",
        "https://pixabay.com/p-1203738/?no_redirect",
        "https://cdn.pixabay.com/photo/2015/11/13/13/45/vw-beetle-1042002_960_720.jpg"
    ];
    var index = document.getElementById('car').selectedIndex;
    document.getElementById('img1').src = pictures[index];

你为什么要用两个链接来设置源代码。我的错。忘了更改它。它应该与数组中的第一个元素相同。同样,您仍然使用两个链接设置源。如果index=0,那么您的源链接将是`“。这是故意的吗?好吧,首先
https://pixabay.com/p-848904/?no_redirect
不是图像。但是图像是否在该url上?每次更改图像时,您都会看到
https://pixabay.com/p-848904/?no_redirect
在新图像路径前面。Fro示例<代码>https://pixabay.com/p-848904/?no_redirecthttp://www.pruebas.pieldetoro.net/web/MERCEDES/DOSSIERES/w115/8-1.jpg首先,为了确保您了解这里发生的事情。。。你没有在任何地方存储图像。您的字符串包含一个URI,该URI告诉浏览器在哪里可以找到图像。实际上,您的问题是“如何在JavaScript数组中存储和访问字符串”。不要使用数组构造函数,而是使用更常见的
var pictures=[]
语法。不要使用方括号表示法和索引,而是使用Array.push。刚刚尝试过,但仍然不起作用:/为什么有“img1”?这是从哪里来的?您只需在表单后添加这一行:
function myFunction()
{
    var pictures = [
        "https://pixabay.com/p-848904/?no_redirect",
        "http://www.pruebas.pieldetoro.net/web/MERCEDES/DOSSIERES/w115/8-1.jpg",
        "https://pixabay.com/p-1203738/?no_redirect",
        "https://cdn.pixabay.com/photo/2015/11/13/13/45/vw-beetle-1042002_960_720.jpg"
    ];
    var index = document.getElementById('car').selectedIndex;
    document.getElementById('img1').src = pictures[index];