如何使用JavaScript向图像src添加参数? 换句话说,我如何将一个参数传递给一个函数,并将它放在一个被定义在同一代码中的SRC的中间?

如何使用JavaScript向图像src添加参数? 换句话说,我如何将一个参数传递给一个函数,并将它放在一个被定义在同一代码中的SRC的中间?,javascript,html,Javascript,Html,注意:请不要使用jQuery。多谢各位 这是我的密码: JavaScript: <script> var icons = document.getElementsByClassName( 'icon' ); // All elements of the "icon" class. var x, y; // Counters. /* The purpose of the below function is to assign images to 8 members of the sa

注意:不要使用jQuery。多谢各位

这是我的密码:

JavaScript:

<script>
var icons = document.getElementsByClassName( 'icon' ); // All elements of the "icon" class.
var x, y; // Counters.

/* The purpose of the below function is to assign images to 8 members of the same 
class, given a piece of the image name from an argument passed to a function. */

function example ( test1, test2, test3, test4, test5, test6, test7, test8 )
{
    for ( x = 0; x < arguments.length, y < (y + 8); x++, y++ )
    {
        icons[y].src = 'http://example.com/img/' + arguments[x] + '.jpg';
    }
}
</script>
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img onclick="example(1, 2, 3, 4, 5, 6, 7, 8)" src="example.jpg" />

var icons=document.getElementsByClassName('icon');//“icon”类的所有元素。
变量x,y;//柜台。
/*以下函数的目的是将图像分配给同一组的8个成员
类,从传递给函数的参数中给定一段图像名称*/
函数示例(test1、test2、test3、test4、test5、test6、test7、test8)
{
对于(x=0;x
HTML:

<script>
var icons = document.getElementsByClassName( 'icon' ); // All elements of the "icon" class.
var x, y; // Counters.

/* The purpose of the below function is to assign images to 8 members of the same 
class, given a piece of the image name from an argument passed to a function. */

function example ( test1, test2, test3, test4, test5, test6, test7, test8 )
{
    for ( x = 0; x < arguments.length, y < (y + 8); x++, y++ )
    {
        icons[y].src = 'http://example.com/img/' + arguments[x] + '.jpg';
    }
}
</script>
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img class="icon" src="" />
<img onclick="example(1, 2, 3, 4, 5, 6, 7, 8)" src="example.jpg" />

现在还不完全清楚为什么它不适合你。我的第一个猜测是您没有正确地构建URL字符串。您正在创建的URL如下所示:

http://example.com/img/1.jpg
http://example.com/img/2.jpg
http://example.com/img/3.jpg
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
此外,您的
for
循环和该循环中的
y
测试还有一些有趣的功能。您可以通过更改此选项来清除(以防导致问题):

function go(/* args go here */) {
    var icons = document.getElementsByClassName("icon");
    for(var x = 0; x < arguments.length; x++)
        icons[x].src = "http://example.com/img/" + arguments[x] + ".jpg";
}

最后,如果您只想将URL创建为一个序列,您只需传递开始和结束序列号:

function go(begin, end) {
    var icons = document.getElementsByClassName("icon");
    for (var x = 0; x < icons.length && begin <= end; x++, begin++)
        icons[x].src = "http://example.com/img/" + begin + ".jpg";
}

go(1, 8);
功能开始(开始、结束){
var icons=document.getElementsByClassName(“图标”);

对于(var x=0;x


你能解释一下你想要达到什么目的吗?

除了大量的参数和奇怪的循环之外,还有什么不起作用呢?试试图标[y]。setAttribute('src',yourURL);如果图像已损坏,则您的URL不正确。使用代码创建的URL如下所示:
http://example.com/img/1.jpg
。您是否100%确定这是正确的URL,因为如果该URL存在并且是一个图像,它应该可以工作。我看不到您在任何地方初始化
y
。我需要一个初始值。另外,t根据定义,
y<(y+8)
的est总是正确的。你想用它做什么。