[英]Make JS functions more efficient and dynamic
I have these 3 functions currently used in a html page: 我目前在html页面中使用了这三个函数:
$('#windowPanel1:first-child').children('div:eq(0)').click(function(){
$('#edit1').val("1");
});
$('#windowPanel1:first-child').children('div:eq(1)').click(function(){
$('#edit1').val("2");
});
$('#windowPanel1:first-child').children('div:eq(2)').click(function(){
$('#edit1').val("3");
});
I would like to have one function that covers all these functions. 我想有一个涵盖所有这些功能的功能。 What would be the most efficient way?
什么是最有效的方式? Thanks!
谢谢!
Bind the event on all the direct descendant div
elements of #windowPanel1:first-child
element. 将事件绑定到
#windowPanel1:first-child
元素的所有直接后代div
元素。 Use index()
to get the index of the clicked element. 使用
index()
获取被点击元素的索引。 The index
returned is zero-based index. 返回的
index
是从零开始的索引。
$('#windowPanel1:first-child > div').click(function() {
$('#edit1').val($(this).index() + 1);
});
>
is direct descendant/child selector which will return same elements as .children('div')
. >
是直接后代/子选择器 ,它将返回与.children('div')
相同的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.