简体   繁体   English

如何使用JavaScript向图像src添加参数?

[英]How to add an argument to an image src using JavaScript?

In other words, how can I take an argument passed to a function, and place it in the middle of an src that is being defined in the same code? 换句话说,如何接受传递给函数的参数,并将其放在同一代码中定义的src的中间?

Note: Please no jQuery . 注意:不要jQuery Thank you. 谢谢。

Here's my code: 这是我的代码:

JavaScript: 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>

HTML: HTML:

<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" />

It is not 100% clear why it isn't working for you. 目前尚不清楚100%为什么对您不起作用。 My first guess would be that you aren't quite building the URL string correctly. 我的第一个猜测是您没有正确构建URL字符串。 The URLs you are creating will look like this: 您创建的URL如下所示:

http://example.com/img/1.jpg
http://example.com/img/2.jpg
http://example.com/img/3.jpg

In addition, there's some funny business with your for loop and the y test in that loop. 此外,您的for循环和该循环中的y测试还有一些有趣的事情。 You can clean that up (in case that's causing an issue) by changing to this: 您可以通过更改为以下内容进行清理(以防引起问题):

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";
}

Here are the changes and why: 以下是更改以及原因:

  1. I removed the y part of the for loop because y < (y + 8) is always true so there's no point in having it as a test for the for loop. 我删除了for循环的y部分,因为y < (y + 8)始终为true,因此没有必要将其作为for循环的测试。
  2. I also never saw any place where you initialized y . 我也从未见过任何初始化y地方。
  3. You were using the comma operator in your condition, not the && operator which also seemed wrong. 您在使用条件时使用了逗号运算符,而不是看起来似乎有误的&&运算符。 Multiple conditions in the if condition should use boolean operators, not the comma operator. if条件中的多个条件应使用布尔运算符,而不是逗号运算符。
  4. x was a global. x是全局的。 I changed it to a local variable since it is initialized and used locally and it's a dangerous name for a global. 我将其更改为局部变量,因为它已在本地初始化和使用,这对于全局变量来说是一个危险的名称。
  5. Changed to icons[x] since y is no longer used. 由于不再使用y因此更改为icons[x]
  6. Moved the definition and initialization of icons into the function since I see no reason it needs to be a global either. icons的定义和初始化移到了函数中,因为我也没有理由认为它也必须是全局的。
  7. Removed the definition of the arguments since the named arguments were not being used. 由于未使用命名参数,因此删除了参数的定义。

If, what you were trying to do is to make sure you never went past the last icon, then your loop can just be this: 如果您想做的是确保您从未超过最后一个图标,那么您的循环就可以是这样:

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

Then, lastly if all you want to do is create the URLs as a sequence, you can just pass the begin and ending sequence numbers: 然后,最后,如果您要做的只是将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);

You can pass the arguments as URL parameters (querystring). 您可以将参数作为URL参数(querystring)传递。

<img src="myImage.png?test1=bla&test2=3&test4=blablala&test5=etc" />

You can then retrieve them like this: 然后,您可以像这样检索它们:

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, " "));
}

https://stackoverflow.com/a/901144/2407212 https://stackoverflow.com/a/901144/2407212

Can you maybe just explain what you are trying to achieve? 您能否解释一下您要实现的目标?

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

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