Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何基于ajax响应隐藏图像标记?_Javascript_Jquery - Fatal编程技术网

Javascript 如何基于ajax响应隐藏图像标记?

Javascript 如何基于ajax响应隐藏图像标记?,javascript,jquery,Javascript,Jquery,什么是正确的jquery语句来替换下面的“//Needed magic”注释,以便根据AJAX响应隐藏或取消隐藏图像标记 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

什么是正确的jquery语句来替换下面的“//Needed magic”注释,以便根据AJAX响应隐藏或取消隐藏图像标记

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>JQuery</title>
<style type="text/css">
    .isSolvedImage{
        width: 68px;
        height: 47px;
        border: 1px solid red;
        cursor: pointer;
    }

</style>
<script src="_js/jquery-1.4.2.min.js" type="text/javascript"> </script>

</head>
<body>

<div id='true1.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='false1.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='true2.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<div id='false2.txt' class='isSolvedImage'>
    <img src="_images/solved.png">
</div>

<script type="text/javascript">

$(function(){
    var getDivs = 0;

    //iterate div with class isSolvedImage
    $("div.isSolvedImage").each(function() {
        alert('div id--'+this.id);
        // send ajax requrest
        $.get(this.id, function(data) {
            // check if ajax response is 1
            alert('div id--'+this.url+'--ajax response--'+data);            
            if(data == 1){
                alert('div id--'+this.url+'--Unhiding image--');
                //Needed magic
                //Show image if data==1 
            }
            else{
                alert('div id--'+this.url+'--Hiding image--');
                //Needed magic
                //Hide image if data!=1
            }
        });

      });
});
</script>
</body>
</html>

JQuery
伊索尔维德先生{
宽度:68px;
高度:47px;
边框:1px纯红;
光标:指针;
}
$(函数(){
var getDivs=0;
//使用类isSolvedImage迭代div
$(“div.isSolvedImage”)。每个(函数(){
警报('div id--'+this.id);
//发送ajax请求
$.get(this.id,函数(数据){
//检查ajax响应是否为1
警报('div id--'+this.url+'-ajax响应--'+data);
如果(数据==1){
警报('div id--'+this.url+'-取消隐藏图像--');
//需要魔法
//如果数据==1,则显示图像
}
否则{
警报('div id--'+this.url+'-隐藏图像--');
//需要魔法
//如果数据!=1,则隐藏图像
}
});
});
});

其中true1.txt是包含要隐藏的图像的div的ID。

我认为最简单的方法是$('.target').hide(); 和$('.target').show();

您可以在此处阅读更多内容:


编辑:我想在回答之前应该先刷新一下

$(this).find('img').show();

$('div[id=“”+$this.url+'“]).show()


$('div[id=“'+$this.url+''“]).hide()

我想你的意思是
$('true1.txt img').hide(),是吗?如果你问我为什么要继续编辑,那是因为我还没有完成。。。我只是将其作为答案发布,即使尚未完成,这样我就可以尽早有时间……)然后继续编辑,直到完成……)谢谢在ajax请求之前创建var img使这变得非常简单。还有一个相关问题,如果我将图像url作为数据返回,而不是返回1或0,我将如何更新图像url而不是隐藏/显示它?img.更新(…数据…);
$('img').hide(); //to hide all image...
$('img').show(); //to show all image...


$(function(){
    var getDivs = 0;

    //iterate div with class isSolvedImage
    $("div.isSolvedImage").each(function() {
        alert('div id--'+this.id);

        var img = $(this).find('img');

        // send ajax requrest
        $.get(this.id, function(data) {
            // check if ajax response is 1
            alert('div id--'+this.url+'--ajax response--'+data);            
            if(data == 1){
                alert('div id--'+this.url+'--Unhiding image--');
                //Needed magic
                //Show image if data==1 
                img.show();
            }
            else{
                alert('div id--'+this.url+'--Hiding image--');
                //Needed magic
                //Hide image if data!=1
                img.hide();
            }
        });

      });
});
$('#true1.txt img').hide();
$(this).find('img').show();
$(this).find('img').hide();