手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >把jQuery的each(callback)方法移植到c#中
把jQuery的each(callback)方法移植到c#中
摘要:$("img").each(function(i){this.src="test"+i+".jpg";});就可以给给所有图像设置src属性...

$("img").each(function(i){

this.src="test"+i+".jpg";

});

就可以给给所有图像设置src属性。

c#中虽然有for(;;)和foreach(..in)可以完成此功能,

staticvoidMain(string[]args)

{

string[]arr=newstring[]{"A","B","C","D","E"};

foreach(stringiteminarr)

{

Console.WriteLine(item);

}

Console.ReadKey();

}

但和jQuery的each(callback)比起来还显得复杂了点。

现在使用c#3.0的扩展方法功能来将each(callback)移植到c#中来。然后我们就可以用这段代码替换上面的了。

staticvoidMain(string[]args)

{

string[]arr=newstring[]{"A","B","C","D","E"};

arr.Each(p=>Console.WriteLine(p));

Console.ReadKey();

}

比foreach简便多了吧,实现代码就几行。

publicdelegatevoidEachDelegate<T>(Targ);

publicstaticclassIEnumerableExtension

{

publicstaticvoidEach<T>(thisIEnumerable<T>src,EachDelegate<T>callback)

{

foreach(Titeminsrc)

{

callback(item);

}

}

}

【把jQuery的each(callback)方法移植到c#中】相关文章:

读写xml所有节点个人小结 和 读取xml节点的数据总结

实现DataGridView控件中CheckBox列的使用实例

.dll 文件反编译的工具软件集合

asp.net gridview强制换行

asp.net图片上传生成缩略图的注意事项

使用HttpWebRequest向网站模拟上传数据

ASP.NET AJAX时用alert弹出对话框

白刃之战:PHP vs. ASP.NET(节选)-架构比较

asp.net下利用JS实现对后台CS代码的调用方法

ASP.NET使用gridview获取当前行的索引值

精品推荐
分类导航