简体   繁体   English

对具有相同ID的元素的操作

[英]Action to element with same id

I need to call a function on items with the same Id with Jquery. 我需要在与Jquery具有相同ID的项目上调用函数。
I tried this: 我尝试了这个:

$(document).ready(function() {
    $('#example').each(function(){
        this.dataTable();
    });
} );

Fiddle 小提琴

IDs should be unique. ID应该是唯一的。 use class and class selectors instead. 改用类和类选择器。

$('.example').each(function(){
    $(this).dataTable();
});

Demo 演示

First of all, ID should be unique. 首先,ID应该是唯一的。 So, use class instead of id. 因此,请使用类而不是id。

Next thing you should bind the jquery to use the method: 接下来,您应该绑定jquery以使用该方法:

$(this).dataTable(); // instead of this, use $(this)

$(document).ready(function(){
    $('.example').each(function(){
        $(this).dataTable();
    });
});

Id s can't be same ,they must be unique Id不能相同,必须unique

you should use class instead of id 您应该使用class而不是id

so 所以

if you give class="example" for all your required elements then you could use 如果为所有必需的元素都提供class="example" ,则可以使用

$(document).ready(function(){
    $('.example').each(function(){
        $(this).dataTable();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM