I'd like to try to fade in and out the image when I press the button, but it doesn't seem to connect well.I think I need an id, but how should I type it?
The following are the current conditions.
As I am a beginner, there are still many things I don't understand, but I appreciate your cooperation.
Prerequisite knowledge
id
declares <button id="hoge">
.#
to express them, such as $("#hoge")
.class
declares <button class="fuga">
..
to express them, such as $(".fuga")
.Code Review
$("#btn-space").click(function(){
, there is no corresponding closure} for function){
.btn-space
is not the button itself, but the div
tag where the button is placed.#appear
is not declared as id
in html.fadeIn
is for.base.css
is not indicated in the question statement.<head>
describes click events, but does the sample of the textbook say so?<body>
as shown in the sample code below?It's important to organize your knowledge of html and javascript and try to guess what's wrong with your code.
Based on the above, please read the text of the textbook again and calmly do your best.
sample code
This is a simple sample code example where clicking the fadeToggle
button causes the image to fade in and out.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
and lt ; title and ; ; / and fade-in and fade-out gt ; and title
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<div>
<div>
<button id="btn-fade">fadeToggle</button>
</div>
<div>
<img class="img-fade" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAI7SURBVFhH7VXPK0RRFPaHKFbKwsJCSiipKQuFhYUSyYJkQxZTipWSUhYWlFIsJMpGLCwkwzSmQfJjaJRfYQozfoxMOPqOd6c71x3mzZv8/upbvHfePfd753z33BRHXip9Jf8F/C4B63UldL3podWqIm1cx6QJcNoy6fEmSEDo0MfPuu9UJrUCO/Z6FgBcOedpKT9N+51MSwKwgVruo6FeQwLR8Uh/VExHSwJOJ4bpKfxAvh575B1EXTrmDAlE3vaGqDUqExbgLs+l8IXf2IbIPztJzuIMjqH/8AHwdB+itRrbm/WClirgKs2moGeZNwLufDvkqSzgGFojTHl/ckCukqw360HLJlwqTOdeCzze3UbKvt1Wa7wlCrgWtKY0LcDb0cSJ1WQ4AeKPgZOxQRYnmxLv5DWgKQHyWUdZ9/s6o847yn/r3eA4cL3uopWyHLqYn+HKyGYVNCUAfUVSGUiM0yB6j78+nx43osRG3WqpjsRVJuQBjFxsgiMoA8MHm6E9e12tHH/wn3IVdHlASyaEsw8GunkTGTiCKPdGY8WH90LcAvBnu53NnFA1IMoOc+IikgFx8nc6xi0g4F400r4OF3gBvUepMWggAt+hPRhK6L0YTO8xbgHoLwwXC8/hMJ+As6lRLn8s06k05QGUHokxaDB8MFzksy8DLdPlUBlTgIAuphL3AgYRhg4uIphSjF4BdY1gUgS8RwFdDIxbgFnEyqPy5whIlAK6GPj9BSQLuj3A7yvgs/jXBaTSC91J3opQUYNpAAAAAElFTkSuQmCC">
</div>
</div>
<script>
$("#btn-fade").click(function(){
$(".img-fade").fadeToggle("slow");
});
</script>
</body>
</html>
© 2024 OneMinuteCode. All rights reserved.