jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery .each() – 实用?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力尝试更好地了解 jQuery.each()方法。这里有一个例子,我想出了,不是很实用,但它对选定的元素执行一个操作从选定的元素集返回:
// Loop over each link.
$( "#links a.number" ).each(

// For each number,run this code. The "inTindex" is the 
// loop iteration index on the current element.
function( inTindex ){

// Bind the onclick event to simply alert the iteration index value.
    $( this ).bind ("click",function(){
        alert( "numbered index: " + inTindex );
    });
});

什么是在你的代码中使用的.each方法的实际使用的一些例子? $(this)代表什么?

解决方法@H_874_8@
注意有两种类型的jQuery的每一个一个迭代和返回jQuery对象,另一个是更通用的版本。

核心/每个
例:页面上创建所有href的csv。 (对匹配的DOM元素进行迭代,“this”表示当前元素)

var hrefs = "";

 $("a").each(function() { 
     var href = $(this).attr('href');
     if (href != undefined && href != "") {
         hrefs = hrefs + (hrefs.length > 0 ? "," + href : href);
     }
 });

 alert(hrefs);

实用程序/ jQuery.each
迭代数组或对象的元素:(通过:
jQuery Documentation)

$.each( { name: "John",lang: "JS" },function(i,n){
  alert( "Name: " + i + ",Value: " + n );
});

$.each( [0,1,2],n){
  alert( "Item #" + i + ": " + n );
});

大佬总结

以上是大佬教程为你收集整理的jQuery .each() – 实用?全部内容,希望文章能够帮你解决jQuery .each() – 实用?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:jquery实用