大佬教程收集整理的这篇文章主要介绍了组合两个相同的 jQuery 函数,但它们使用不同的选择器和类,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个函数做同样的事情,除了我定位的元素不同,我想添加的 ID 也是如此。将这些组合成一个函数的最佳方法是什么?
<script>
$('.Index-page--has-image[ID*="angle-right"]').each(function(){
var index = $(this).index('.Index-page--has-image')
$('.Parallax-host .Parallax-item').eq(indeX).attr('ID','angle-right');
});
$('.Index-page--has-image[ID*="angle-left"]').each(function(){
var index = $(this).index('.Index-page--has-image')
$('.Parallax-host .Parallax-item').eq(indeX).attr('ID','angle-left');
});
</script>
['left','right'].forEach( way => {
way = 'angle-' + way ;
$('.Index-page--has-image[id*="'+way+'"]').each(function(){
var index = $(this).index('.Index-page--has-image');
$('.Parallax-host .Parallax-item').eq(indeX).attr('id',way);
});
} );
,
提取JQ each
函数外的函数,并将angle
值传递给函数参数,像这样
function yourFunctionName(element,idvalue){
let index = $(element).index('.Index-page--has-image')
$('.Parallax-host .Parallax-item').eq(indeX).attr('id',idvalue);
}
$('.Index-page--has-image[id*="angle-right"]').each(function(){
yourFunctionName(this,'angle-right');
});
$('.Index-page--has-image[id*="angle-left"]').each(function(){
yourFunctionName(this,'angle-left');
});
以上是大佬教程为你收集整理的组合两个相同的 jQuery 函数,但它们使用不同的选择器和类全部内容,希望文章能够帮你解决组合两个相同的 jQuery 函数,但它们使用不同的选择器和类所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。