Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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 我们如何在jquery中对“This”事件应用条件?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 我们如何在jquery中对“This”事件应用条件?

Javascript 我们如何在jquery中对“This”事件应用条件?,javascript,jquery,html,Javascript,Jquery,Html,此代码用于通过幻灯片和确认消息以jquery语言显示文本和图片。在这段代码中,我遇到了一个问题,即通过id隐藏图片调用时使用的乐趣是,当我们说ok时,它会隐藏图片,否则它不会隐藏图片 <!DOCTYPE html> <html> <head> <title></title> <!--we add jquery library file--> <script src="test.js"></script>

此代码用于通过幻灯片和确认消息以jquery语言显示文本和图片。在这段代码中,我遇到了一个问题,即通过id隐藏图片调用时使用的乐趣是,当我们说ok时,它会隐藏图片,否则它不会隐藏图片

<!DOCTYPE html>
<html>
<head>
<title></title>
<!--we add jquery library file-->
<script src="test.js"></script>
<!--fun applied on text & images-->
<script>
//apply on text
 $(document).ready(function() {
        $('h1,h2').show('slow')
    })
//apply on image that call by class
    $(document).ready(function() {
        $('.img').click(function()
                    {
                        alert("You did not hide this Picture!");
                    }
            )
    })
$(document).ready(function() 
{
$('#img').click(function() 
{
    let shouldHide = confirm("Are You Sure To Hide This Picture?");
    if (shouldHide)  
{
        $(this).hide('slow');
    } else 
 {
        return;
    }
})
 })
</script>
<!--apply  to clean the screen-->
<style type="text/css">
    h1,h2
    {
        display: none;
    }
</style>
</head>
<body>
   <!--display the image with class and id-->
   <img src="images/1.jpg" width="400" height="500" id="img">
   <img src="images/2.jpg" width="400" height="500" class="img">
   <!--Display text by slide-->
   <h1>How Are you?</h1>
   <h2>I'm fine....</h2>
</body>
</html>
您可以使用JavaScript的confirm方法

    $('#img').click(function () {
        if(confirm("Are you sure?"))
        {
            $(this).hide('slow');
        }
    });
此外,当不在循环中时,也不能使用break


告诉我它是否对你有用

您不需要编写两个相同的函数,只需编写一个函数即可

//apply on text
 $(document).ready(function() {
        $('h1,h2').show('slow')
    })
//apply on image that call by class
    $(document).ready(function() {
        $('.img').click(function()
                    {
                        alert("You did not hide this Picture!");
                    }
            )
    })
    $(document).ready(function() {
        $('#img').click(function () {
            if(confirm("Are You Sure To Hide This Picture?"))
            {
                $(this).hide('slow');
            }
        })
    })

确认返回需要决定是否隐藏的布尔值,不能使用break;在函数内部,应该使用return;相反

<!DOCTYPE html>
<html>
<head>
<title></title>
<!--we add jquery library file-->
<script src="test.js"></script>
<!--fun applied on text & images-->
<script>
//apply on text
$(document).ready(function() {
        $('h1,h2').show('slow')
    })
    //apply on image that call by class
$(document).ready(function() {
        $('.img').click(function() {
            alert("You did not hide this Picture!");
        })
    })
    //apply on image that call by id
$(document).ready(function() {
    $('#img').click(function() {
        let shouldHide = confirm("Are You Sure To Hide This Picture?");
        if (shouldHide) {
            $(this).hide('slow');
        } else {
            return;
        }
    })
})
</script>
<!--apply  to clean the screen-->
<style type="text/css">
    h1,h2
    {
        display: none;
    }
</style>
</head>
<body>
   <!--display the image with class and id-->
   <img src="images/1.jpg" width="400" height="500" id="img">
   <img src="images/2.jpg" width="400" height="500" class="img">
   <!--Display text by slide-->
   <h1>How Are you?</h1>
   <h2>I'm fine....</h2>
</body>
</html>