Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:如何获取元素索引的名称?_Javascript - Fatal编程技术网

JavaScript:如何获取元素索引的名称?

JavaScript:如何获取元素索引的名称?,javascript,Javascript,需要从元素数组中获取元素的索引号 <input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 0--> <input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 1--> <input type="button" name="test"

需要从元素数组中获取元素的索引号

<input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 0-->
<input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 1-->
<input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 2-->
<input type="button" name="test" value="test" onclick="getindex(this)"> <!--index 3-->
...
<script>
function getindex(obj) {
    var i = obj.index; //here is wrong. How to get index?
}
</script>

...
函数getindex(obj){
var i=obj.index;//这里是错误的。如何获取索引?
}

更改页面时不太容易出现问题的方法是将值分配给数据属性:

<input type="button" name="test" value="test" onclick="getindex(this)" data-index="0"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="1"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="2"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="3"> 
<script>
function getindex(obj) {
    var i = parseInt(obj.dataset.index,10);
}
</script>

然后从该数据属性检索索引:

<input type="button" name="test" value="test" onclick="getindex(this)" data-index="0"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="1"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="2"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="3"> 
<script>
function getindex(obj) {
    var i = parseInt(obj.dataset.index,10);
}
</script>

函数getindex(obj){
var i=parseInt(obj.dataset.index,10);
}

更改页面时不太容易出现问题的方法是将值分配给数据属性:

<input type="button" name="test" value="test" onclick="getindex(this)" data-index="0"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="1"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="2"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="3"> 
<script>
function getindex(obj) {
    var i = parseInt(obj.dataset.index,10);
}
</script>

然后从该数据属性检索索引:

<input type="button" name="test" value="test" onclick="getindex(this)" data-index="0"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="1"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="2"> 
<input type="button" name="test" value="test" onclick="getindex(this)" data-index="3"> 
<script>
function getindex(obj) {
    var i = parseInt(obj.dataset.index,10);
}
</script>

函数getindex(obj){
var i=parseInt(obj.dataset.index,10);
}

因为元素本身不知道任何索引,所以这种方法是不可能的。解决此问题的另一种方法是动态创建按钮:

<script>
    for (var i = 0; i < 4; i++) {
      var onClick = function () {
        console.log(i);
      }

      var button = document.createElement('button');
      button.onclick = onClick;
      document.querySelector('body').appendChild(button); 
    }
</script>

对于(变量i=0;i<4;i++){
var onClick=函数(){
控制台日志(i);
}
var button=document.createElement('button');
button.onclick=onclick;
document.querySelector('body').appendChild(按钮);
}

因为元素本身不知道任何索引,所以这种方法是不可能的。解决此问题的另一种方法是动态创建按钮:

<script>
    for (var i = 0; i < 4; i++) {
      var onClick = function () {
        console.log(i);
      }

      var button = document.createElement('button');
      button.onclick = onClick;
      document.querySelector('body').appendChild(button); 
    }
</script>

对于(变量i=0;i<4;i++){
var onClick=函数(){
控制台日志(i);
}
var button=document.createElement('button');
button.onclick=onclick;
document.querySelector('body').appendChild(按钮);
}

添加一个名为index to html button的属性,然后单击按钮尝试访问它。你可以用这个

<input type="button" index='0' name="test" value="test" onclick="getindex(this)"> <!--index 0-->
<input type="button" index='1' name="test" value="test" onclick="getindex(this)"> <!--index 1-->
<input type="button" index='2' name="test" value="test" onclick="getindex(this)"> <!--index 2-->
<input type="button" index='3' name="test" value="test" onclick="getindex(this)"> <!--index 3-->

<script>
    function getindex(obj) {
        var i = obj.getAttribute('index');
    }
</script>

函数getindex(obj){
var i=obj.getAttribute('index');
}

添加一个名为index to html button的属性,然后单击按钮尝试访问它。你可以用这个

<input type="button" index='0' name="test" value="test" onclick="getindex(this)"> <!--index 0-->
<input type="button" index='1' name="test" value="test" onclick="getindex(this)"> <!--index 1-->
<input type="button" index='2' name="test" value="test" onclick="getindex(this)"> <!--index 2-->
<input type="button" index='3' name="test" value="test" onclick="getindex(this)"> <!--index 3-->

<script>
    function getindex(obj) {
        var i = obj.getAttribute('index');
    }
</script>

函数getindex(obj){
var i=obj.getAttribute('index');
}

您不需要添加属性


函数getindex(index){
警报(索引);
}

您不需要添加属性


函数getindex(index){
警报(索引);
}

你所说的
索引是什么意思
?你无法为你正在尝试的内容获取索引。尝试为输入提供ID,并像obj.id一样访问它。为什么不在html中单击“getindex”时传递索引?您需要在getindex()函数中传递索引值。您是否试图在没有任何附加数据的情况下获取数组的索引?您所说的
索引是什么意思?您无法为正在尝试的内容获取索引。尝试为输入提供ID,并像obj.id一样访问它。为什么不在html中单击“getindex”时传递索引?您需要在getindex()函数中传递索引值。是否尝试在没有任何附加数据的情况下获取数组的索引?只需将索引传递给函数即可将索引传递给函数