Javascript 我可以将按钮的“id”设置为变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5444640/
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
Can I set "id" of button to a variable?
提问by Camellia
I want to set a button's id to a variable, so that I can easily get access to changing numbers of buttons:
我想将按钮的 id 设置为一个变量,以便我可以轻松访问更改按钮数量:
<script>var test = 1; </script>
<button id=test> click me </button>
but it is does not work. my motivation to do this : I read from a xml file and display part of them using form. and then I have to change the value of them. and the number of items I read from xml is not fixable. so if i want to control them i need give each item a number.
但它不起作用。我这样做的动机:我从一个 xml 文件中读取并使用表单显示其中的一部分。然后我必须改变它们的价值。并且我从 xml 中读取的项目数量无法修复。所以如果我想控制它们,我需要给每个项目一个编号。
回答by harpo
var test=1;
var ele = document.getElementById('test');
ele.id="button_" + test;
ele.value="button " + test;
Just to be clear, when you say
只是要清楚,当你说
<button id=test/>
or, for that matter, the more standards-compliant
或者,就此而言,更符合标准
<button id="test"/>
the word test
is just a text identifier. It is not interpreted as javascript.
这个词test
只是一个文本标识符。它不会被解释为 javascript。
The only place (I know of) where you can put javascript directly into an HTML attribute and it will be interpreted, is an event handler (such as onclick
) or in the href
of an a
tag, after the prefix javascript:
.
唯一的地方(我知道),你可以把JavaScript的直接插入HTML属性,它会被解释,是事件处理程序(如onclick
),或在href
一个的a
标签,前缀之后javascript:
。
回答by Bene
var button = document.getElementsById("test");
Your var 'button' will contain the DOM object with the id 'test'. You might wanna do this a little more 'dynamic' something like:
您的 var 'button' 将包含 ID 为 'test' 的 DOM 对象。您可能想要更“动态”地执行此操作,例如:
var myArray = [];
var oButton = document.getElementsByTagName("button");
var i = 0;
for(i=0;i<oButton.length;i++) {
if (oButton[i].className == 'likedButton') {
myArray.push(oButton[i]);
}
}
This way, you simply need to add "class='likedButton'" to your button and this will add all those button into the array 'myArray'. Next step if you wanna do anything to those button, just go throught the 'myArray' object myArray[0] showing the first object with the class 'likedButton'. I suggested the class attribute so that you can easily find tons of button. If you use the ID attribute, all IDs MUST be different.
这样,您只需将“class='likedButton'”添加到您的按钮,这会将所有这些按钮添加到数组“myArray”中。下一步,如果您想对这些按钮执行任何操作,只需通过“myArray”对象 myArray[0] 显示第一个带有“likedButton”类的对象。我建议使用 class 属性,以便您可以轻松找到大量按钮。如果您使用 ID 属性,则所有 ID 必须不同。
回答by Wala Wala
You cannot change it immediatly inside of the Button ID. Have to change it after the Button.
您不能在按钮 ID 内立即更改它。必须在按钮之后更改它。
var button3 = "button3"+i
var button4 = "button4"+i
'<button id="button3" type="submit" class="accpet-button">Accept</button></td><td>'+
'<button id="button4" class="cancel-button" data-dismiss="modal">Cancel</button></td>'
+"</tr>");
var ele = document.getElementById('button3');
ele.id="button3"+i;
var ele = document.getElementById('button4');
ele.id="button4"+i;
i++;
document.getElementById(button3).disabled = button1;
document.getElementById(button4).disabled = button2;
回答by T.J. Crowder
What you've done in your code is assign the id
value "test" to the element.
您在代码中所做的是将id
值“test”分配给元素。
It sounds like you're already dealing directly with DOM elements, so you can just assign the value to their id
property. Example:
听起来您已经在直接处理 DOM 元素,因此您只需将值分配给它们的id
属性即可。例子:
var list, li, index;
// Find a list that already exists on the page
list = document.getElementById('myList');
// Add four items to it
for (index = 0; index < 4; ++index) {
li = document.createElement('li');
li.id = "listItem" + index; // listItem0, listItem1, ...
li.innerHTML = "I'm list item #" + index;
list.appendChild(li);
}
回答by mcgrailm
I don't think you can directly set the dom objects id to to a javascript variable but you can can change the id using javascript or jQuery I'm not totally clear on what your trying to do. but I htink what your saying is that you want to change id based on the number of buttons to do that use jquery foreach
我认为您不能直接将 dom 对象 id 设置为 javascript 变量,但您可以使用 javascript 或 jQuery 更改 id 我不完全清楚您要做什么。但我认为你的意思是你想根据按钮的数量更改 id 使用 jquery foreach
$('button').each(function(key,value){
$(this).attr("id","button_"+key)
});