function Psend()
 {

     new Ajax.Request('Handler.ashx',
        {
            method: 'get',
            onSuccess: function(transport) {
                var response = transport.responseText || "no response text";
                //alert("Success! \n\n" + response);
                var obj = response.evalJSON(true);

                for (i = 0; i < 4; i++) {

                    DeCheBX = $('MyDiv').insert(new Element('input', { 'type': 'checkbox', 'id': "img" + obj[i].Nam, 'value': obj[i].IM, 'onClick': 'SayHi(this,i)' }));
                    DeImg = $('MyDiv').insert(new Element('img', { 'id': "img" + obj[i].Nam, 'src': obj[i].IM, 'style': 'display = inline', 'onClick': 'Say(this)' }));
                    document.body.appendChild(DeCheBX);
                    document.body.appendChild(DeImg);

                }

            },
            onFailure: function() { alert('Something went wrong...') }
        });


        SayHi = function(x,i) {

            if ($(x).checked == true) {

                //               $('id').hide();

                **$('img'+i).style.visibility = "hidden";**// doesnt work
            }



        };


Handler.ashx

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        string[] Img = new string[5] { "http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg", "http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg", "http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg", "http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg", "http://farm3.static.flickr.com/2195/2214604053_1de19931cf_t.jpg" };
        int[] Name = new int[5] { 1, 2, 3, 4, 5 };
        StringBuilder output = new StringBuilder();
        // output.Append("\"Images\":\" ");
        output.Append("[");
        for (int i = 0; i < 5; i++)
        {
            output.Append("{");
            output.Append("\"Nam\":\"" + Name[i].ToString() + "\",");
            output.Append("\"IM\":\"" + Img[i] + "\" ");
            if (i != 4)
            {
                output.Append("},");
            }
        }

        output.Append("}]");
        context.Response.Write(output);



    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}


输出

<input id="Button1" value="button" onclick=" Psend()" type="button">
<div id="MyDiv">

<input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg" id="img1" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3210/3033577103_f80cb2e399_t.jpg" id="img1"><input onclick="SayHi(this)" value="http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg" id="img2" type="checkbox"><img onclick="Say(this)" style="" src="http://farm1.static.flickr.com/76/184936863_dceeaa048c_t.jpg" id="img2"><input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg" id="img3" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3133/2630880079_9035711f2f_t.jpg" id="img3"><input onclick="SayHi(this)" value="http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg" id="img4" type="checkbox"><img onclick="Say(this)" style="" src="http://farm4.static.flickr.com/3064/2395929114_a4d69a22c6_t.jpg" id="img4">
</div>

最佳答案

$('img'+i)


i未定义。

更新:您正在将元素ID设置为"img" + obj[i].Nam

关于javascript - 如何隐藏图像?我能怎么做?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2778897/

10-08 21:28