I am new to jQuery and am trying to figure out how to repeat functions. As you can see from the code below when clicked on the button it will turn to 'I was clicked' in an h1 and then when clicked again it will go back to 'Click Me' however of course it stops after that. Without having to continuously write those lines over and over again, what is the easiest way to have it repeat so anytime I click on it it changes?
$("button").click(function() { $(this).html("<h1>I was clicked</h1>"); $(this).click(function() { $(this).html("Click Me") }) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Hello</h1> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button>
没有评论:
发表评论