Javascript 如何在javascript中将html id元素设置为变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6940497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set html id element as variable in javascript
提问by Saurabh Kumar
var html ='<li id="existingProductArray[i]">'
+ '<input type="image" id="existingProductArray[i]" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
+ existingProductArray[i]
+ '</li>';
I saw in the firebug the id is like this where as i want the value of existingProductArray[i] to be the id.Where I am doing wrong.
我在萤火虫中看到 id 是这样的,因为我希望 existingProductArray[i] 的值是 id。我做错的地方。
id="existingProductArray[i]"
回答by iConnor
Try this
尝试这个
var id = existingProductArray[i];
var html ='<li id="' + id + '">'
+ '<input type="image" id="' + id + '" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
+ id
+ '</li>';
But
但
ID hence the name should be unique you are giving 2 elements the same ID ( that's a bad idea )
ID 因此名称应该是唯一的,您给 2 个元素提供了相同的 ID(这是一个坏主意)
回答by Alex
Try changing:
尝试改变:
+ '<input type="image" id="existingProductArray[i]" src="...>'
+ '<input type="image" id="existingProductArray[i]" src="...>'
to
到
+ '<input type="image" id="'+existingProductArray[i]+'" src="...>'
+ '<input type="image" id="'+existingProductArray[i]+'" src="...>'
So in your line of code it was just using it as text string. You need to break out of the string and do it.
因此,在您的代码行中,它只是将其用作文本字符串。你需要摆脱束缚并做到这一点。
回答by Michael Berkowski
You just need to close the quotes and concatenate it in with +
:
您只需要关闭引号并将其连接到+
:
var html ='<li id="existingProductArray[i]">'
+ '<input type="image" id="' + existingProductArray[i] + '" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
+ existingProductArray[i]
+ '</li>';
回答by Sherif elKhatib
your reference is inside the quotations
您的参考在引文内
var html ='<li id="'+existingProductArray[i]+'">'
+ '<input type="image" id="'+existingProductArray[i]+'" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
+ existingProductArray[i]
+ '</li>';
回答by DBJoran
I stumbled upon the same problem, I know my problem is not 100% the same as yours but I bet I can help other people out who search for this kind of problem.
我偶然发现了同样的问题,我知道我的问题与您的问题并非 100% 相同,但我敢打赌我可以帮助其他寻找此类问题的人。
I was trying to add a variable as a ID name for a div. I tried several methods mentioned above but none of those seemed to work.
我试图添加一个变量作为 div 的 ID 名称。我尝试了上面提到的几种方法,但这些方法似乎都不起作用。
I did the following:
我做了以下事情:
div.id = array[i];
div.id = array[i];
which in your case would have simply been:
在您的情况下,这只是:
id=existingProductArray[i];
No " " or + needed, don't forget to declare the i
variable. This might be very obvious for some people with more experience.
不需要“”或+,不要忘记声明i
变量。对于一些有更多经验的人来说,这可能非常明显。