简体   繁体   English

如何删除动态创建的 <div> 如果在asp.net C#中img src =&#39;undefined&#39;是否标记?

[英]How to remove dynamicaly created <div> tag if img src='undefined' in asp.net C#?

I am trying to select folder content.This folder contain images when I am selecting that folder it will select all content.plus it is taking img src undefind. 我正在尝试选择文件夹内容。当我选择该文件夹时,该文件夹包含图像,它将选择所有内容。另外,它正在使用img src undefind。 so I need to remove that how can I remove if src is undefined using asp.net c# 所以我需要删除如果使用asp.net c#未定义src如何删除

<div id="cell" class="box2">
    <a href="undefined">
        <img width="260px" height="135px" src="undefined"
            alt="" 
            style="box-shadow: 1px    2px 2px #BDBDBD;
            border: 1px solid #D1D1D1;">
       </img>
    </a>
</div>

Code behind file: 文件后面的代码:

protected void chbindustry_SelectedIndexChanged(object sender, EventArgs e)
{
    if (result == false)
    {
        string[] subdirectoryEntries = Directory.GetDirectories(Server.MapPath("BusinessCards"));
        string f;
        string[] ss;
        string side = chklist.SelectedValue;// RadioButtonList1.SelectedValue;
        foreach (ListItem li in chbindustry.Items)
        {
            if (li.Selected)
            {

                ss = li.Text.Split('(');

                f = Server.MapPath("BusinessCards").ToString() + "\\" + ss[0];
                int c = f.Count();
                DirectoryInfo d = new DirectoryInfo(f);
                int len = d.GetFiles().Length;
                for (int i = 1; i <= d.GetFiles().Length / 3; i++)
                {
                    Page.ClientScript.RegisterArrayDeclaration("ImgPaths", "'" + "BusinessCards/" + f.Remove(0, f.LastIndexOf('\\') + 1) + "/" + i + ".jpg'");
                    Page.ClientScript.RegisterArrayDeclaration("refs", "'" + "DesignBCs.aspx?img=BusinessCards/" + f.Remove(0, f.LastIndexOf('\\') + 1) + "/" + i + "&Side=" + side + "'");
                }
            }
        }
    }
    result = true;
}

Dynamically creating div: 动态创建div:

<script type="text/jscript">
    $(document).ready(function () {

        for (var i = 0; i < ImgPaths.length; i = i + 3) {
            var rowdiv = '<div id="row">';
            rowdiv = rowdiv + ' <div id="cell" class="box2">';
            rowdiv = rowdiv + ' <a href="' + refs[i] + '"><img width="260px" style="box-shadow: 1px 2px 2px #BDBDBD; border: 1px solid #D1D1D1;" height="135px" alt="" src="' + ImgPaths[i] + '"></img></a> ';
            rowdiv = rowdiv + ' </div>';
            rowdiv = rowdiv + ' <div id="cell" class="box2">';
            rowdiv = rowdiv + '  <a href="' + refs[i + 1] + '"><img width="260px" style="box-shadow: 1px 2px 2px #BDBDBD; border: 1px solid #D1D1D1;" height="135px" alt="" src="' + ImgPaths[i + 1] + '"></img></a>';
            rowdiv = rowdiv + '</div>';
            rowdiv = rowdiv + ' <div id="cell" class="box2">';
            rowdiv = rowdiv + ' <a href="' + refs[i + 2] + '"><img width="260px" style="box-shadow: 1px 2px 2px #BDBDBD; border: 1px solid #D1D1D1;" height="135px" alt="" src="' + ImgPaths[i + 2] + '"></img></a>';
            rowdiv = rowdiv + '</div>';
            rowdiv = rowdiv + '</div>';
            $("#table").append(rowdiv);
        }
    });
</script>

Handle the onError event for the image to call JavaScript function which will remove the div: 处理图片的onError事件以调用JavaScript函数,该函数将删除div:

function imgError(divid) {
  document.getElementById(divid).style.display = 'none';
    return true;
}


<img src="image.png" onerror="imgError(divid);"/>

you need to pass div id to function which you want to hide. 您需要将div id传递给要隐藏的功能。

If you don't mind using JQuery, this would work for you. 如果您不介意使用JQuery,这将为您工作。 See DEMO HERE 这里查看演示

$(document).ready(function () {
    $('img[src="undefined"]').remove();
});

Updated DEMO to remove div. 更新了DEMO以删除div。

$('img[src="undefined"]').parent().parent().remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM