如何在jquery中找到图像源

本文关键字:图像 jquery | 更新日期: 2023-09-27 18:14:11

我有一个GridView,有一个列显示图像。问题是它在所有列中显示最后一个图像,所以我需要在jquery中设置图像src,但我不确定如何。我在c#中做了类似的事情:

 protected void gvMaintenance_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Maintenance main = (Maintenance)e.Row.DataItem;
    if (!string.IsNullOrEmpty(main.Image))
            {
                lblMainImage.Visible = false;
                imgMainImage.Visible = true;
                imgMainImage.ImageUrl = "~/MaintenanceImages/" + Company.Current.CompCode + "/" + main.Image;
                imgMainImage.Width = 200;
                imgMainImage.Height = 100;
                imgMainImage.Attributes.Add("href", "/MaintenanceImages/" + Company.Current.CompCode + "/" + main.Image);
            }
}

但是我如何在jquery中做到这一点?

<asp:Image runat="server" ID="imgMainImage" />
 $("#<%= imgMainImage.ClientID %>").attr("src", "");

如何在jquery中找到图像源

你不需要jQuery,如果列有一个唯一的路径为每个图像,那么指定的行应该反映唯一的图像。根据我看到的代码,问题是你添加了一个Attributes.Add,但你确定你已经找到了合适的Control吗?

另一件要注意的事情是,你有什么网格事件的图像被内置?

以上是一些建议,如果没有更多的代码,我们将无法提供帮助。

gvMaintenance_RowDataBound中的代码需要被删除,我能够在jquery中使用以下方式链接图像:

 $("#<%= imgMainImage.ClientID %>").attr("src", "<%= "/MaintenanceImages/" + Company.Current.CompCode + "/" %>" + $(sender).children().eq(14).html());