减少用于加速交互的jquery选择器的数量

Reduce the amount of jquery selectors being used to speed up interactions

本文关键字:jquery 选择器 交互 用于 加速      更新时间:2023-09-26

我有一个有多个选择器的页面。每个都有一个点击事件。我只是想知道是否有办法将这些选择器组合起来或者甚至做一个全局选择器让jQuery找到被点击的内容?

"做一个全局选择器,让jQuery找到被点击的内容?"


$('select').click ( function () {
    console.log ('A ', this.nodeName, ' was clicked'n',
        'It had an id of: ', this.id
    );
    /* Etc., etc.  You can use jQuery or standard DOM to 
        tell most anything about the element that was clicked.
    */
} );

如果我理解对了你的问题,你要做的是通过给它们一个类。my_selector_class来对它们进行分组。

然后添加一个点击事件

$('.my_selector_class').click(function(){
})

和内部的$(this)选择器一起工作,只反映实际单击元素的更改。

这对性能的最大作用是通过减少加载时获取的字节来加快页面加载时间。