0 .net javascript c# asp.net jquery
我的ASP.NET页面上有一堆图像,如下所示:
for (int i = 0; i < 3; i++)
{
Image image = new Image();
image.ID = "UpdateStatus";
image.CssClass = "imageCss";
image.ImageUrl = "Bump.GIF";
this.Controls.Add(image);
}
Run Code Online (Sandbox Code Playgroud)
我想遍历每个图像.
为什么这不起作用?即输出"功能1"但不输出"功能2"
$(document.body).click(function () {
console.log('Function 1');
$('imageCss').each(function(){
console.log('Function 2');
});
});
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?即输出"功能1",输出"功能2"ONCE.为什么不3次?
$(document.body).click(function () {
console.log('Function 1');
$(''#UpdateStatus'').each(function(){
console.log('Function 2');
});
});
Run Code Online (Sandbox Code Playgroud)
这工作正常但是我无法访问'this',它是NULL.我需要能够引用当前图像.
$(document.body).click(function () {
console.log('Function 1');
$.each('#UpdateStatus', function() { //Or when I use 'imageCss'
console.log('Function 2');
console.log(this);
});
});
Run Code Online (Sandbox Code Playgroud)
Rex*_*x M 10
(1)您的jQuery调用$('imageCss')不是有效的选择器.你需要在类名前加上'.'.表示您要选择类"imageCss"的元素:
$('.imageCss')
(2)ID必须是唯一的.在ASP.NET中的服务器上设置Control.ID ="foo"时,即设置控件的服务器端 ID.HTML中生成的客户端ID将是一个独特,冗长,丑陋的字符串ctl00_ctl01_pnlMain_foo1.您最好的选择是使用类选择器.您可以使用空格将多个类应用于单个元素:
myImage.CssClass = "image otherclass";
Run Code Online (Sandbox Code Playgroud)
双方$('.image')并$('.otherclass')会正确地选择元素.
(3)this因封闭而不存在.函数在调用函数的对象的上下文中执行,因此this将是该对象.
或者,在你的情况下,它看起来并不像你的函数在任何对象的上下文中执行,所以我不确定你期望this的是什么.