Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

javascript - 如何将字符串与变量连接?(How do I concatenate a string with a variable?)

So I am trying to make a string out of a string and a passed variable(which is a number).(所以我试图用字符串和传递的变量(这是一个数字)来创建一个字符串。)

How do I do that?(我怎么做?) I have something like this:(我有这样的事情:) function AddBorder(id){ document.getElementById('horseThumb_'+id).className='hand positionLeft' } So how do I get that 'horseThumb' and an id into one string?(那么如何将'horseThumb'和id变成一个字符串呢?) I tried all the various options, I also googled and besides learning that I can insert a variable in string like this getElementById("horseThumb_{$id}") <-- (didn't work for me, I don't know why) I found nothing useful.(我尝试了各种各样的选项,我也google了,除了学习我可以在字符串中插入一个变量,如getElementById("horseThumb_{$id}") < - (对我来说不起作用,我不知道为什么)我发现没什么用处。) So any help would be very appreciated.(所以任何帮助都将非常感激。)   ask by necker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is correct.(你的代码是正确的。)

Perhaps your problem is that you are not passing an ID to the AddBorder function, or that an element with that ID does not exist.(也许您的问题是您没有将ID传递给AddBorder函数,或者具有该ID的元素不存在。) Or you might be running your function before the element in question is accessible through the browser's DOM.(或者您可能在通过浏览器的DOM访问相关元素之前运行您的函数。) To identify the first case or determine the cause of the second case, add these as the first lines inside the function:(要识别第一种情况或确定第二种情况的原因,请将这些添加为函数内的第一行:) alert('ID number: ' + id); alert('Return value of gEBI: ' + document.getElementById('horseThumb_' + id)); That will open pop-up windows each time the function is called, with the value of id and the return value of document.getElementById .(这将在每次调用函数时打开弹出窗口,其值为iddocument.getElementById的返回值。) If you get undefined for the ID number pop-up, you are not passing an argument to the function.(如果undefined ID号弹出窗口,则不会将参数传递给该函数。) If the ID does not exist, you would get your (incorrect?) ID number in the first pop-up but get null in the second.(如果ID不存在,您将在第一个弹出窗口中获得(错误的?)ID号,但在第二个弹出窗口中获取null 。) The third case would happen if your web page looks like this, trying to run AddBorder while the page is still loading:(如果您的网页看起来像这样,在页面仍在加载时尝试运行AddBorder ,则会发生第三种情况:) <head> <title>My Web Page</title> <script> function AddBorder(id) { ... } AddBorder(42); // Won't work; the page hasn't completely loaded yet! </script> </head> To fix this, put all the code that uses AddBorder inside an onload event handler:(要解决此问题,请将使用AddBorder的所有代码放在onload事件处理程序中:) // Can only have one of these per page window.onload = function() { ... AddBorder(42); ... } // Or can have any number of these on a page function doWhatever() { ... AddBorder(42); ... } if(window.addEventListener) window.addEventListener('load', doWhatever, false); else window.attachEvent('onload', doWhatever);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...