[英]How I Can Conver Variable value in Element id in Javascript
If I declare value in A
this value I want to access as id in B
through getElementById()
.如果我在
A
声明了这个值,我想通过getElementById()
在B
作为 id 访问。 Can I use this method?我可以使用这种方法吗?
var A = 10;
var B = doument.getElementbyId(A)
console.log(B)
Case-sensitivity matters in JavaScript. JavaScript 中区分大小写很重要。
id
param of getElementById
should be a case-sensitive string getElementById
的id
参数应该是区分大小写的字符串You can convert the integer to a string by calling toString()
on it.您可以通过调用
toString()
将整数转换为字符串。
var A = 10; var B = document.getElementById(A.toString()) console.log(B)
<div id="10">Hello World</div>
That's not how you are supposed to do that.这不是你应该这样做的方式。
document.getElementById
returns the HTML element with that particular id. document.getElementById
返回具有该特定 id 的 HTML 元素。
This is done using Objects
.这是使用
Objects
完成的。
Try this:试试这个:
var A = 10;
var B = {
id: A
}
console.log(B.id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.