我正在使用jquery来实现在同一个jsp中重复多个位置的一些功能。我想再次使用相同的jquery代码。任何人都可以告诉我如何实现这一目标?希望下面的代码片段解释我真正想要实现的目标。
<div id="1" class="a">
//implement the some functionality to all the
elements that below to this class and id="1"
</div>
<div id="2" class="a">
//implement the similar functionality that is happening
int above div to all the elements that below to this class and id="2"
</div>
<div id="3" class="a">
//implement the similar functionality that is happening
int above div to all the elements that below to this class and id="2"
</div>
<script type="text/javascript">
//Re-usable code goes here that applies for class type 2 and tracks id 1,2,3 differently.
</script>
修改
我想在同一个html页面的多个地方使用datatable,并且在使用的数据表的每个实例上,我想使用Jquery进行类似的自定义。
答案 0 :(得分:2)
我认为这就是你所追求的。当单击任何具有id 1,2或3的元素时,它将执行相同的功能。
$(document).on('click', '#1, #2, #3', function (e) {
console.log(e.target); // This is the element that was clicked
alert('Clicked')
});
https://jsfiddle.net/cnLvk3o1/1/
如果你想按类选择A.Wolff在下面提到过。你可以改用它。
$(document).on('click', '.a', function (e) {
console.log(e.target); // This is the element that was clicked
alert('Clicked')
});