4
22/10/2024 4:24 pm
Início do tópico
Use jQuery to write a JS function that loads an online resource into a div with id "text". Make a request to "text.html" and replace the target div’s content with the returned HTML. Create the files ajax-load.html and text.html and place them in the same folder, so that your script can find the latter.
HTML Skeleton:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AJAX Load Example</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div id="text">
<h1>AJAX jQuery.load()</h1>
<button onclick="loadTitle()">Load Title</button>
</div>
</body>
</html>
text.html:
<h1>Voilla!</h1> <p>I am a text loaded with AJAX request</p>
Examples:

1 Resposta
3
22/10/2024 4:25 pm
Here is the text.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Voilla!</h1>
<p>I am a text loaded with AJAX request</p>
</body>
</html>
And the solution (HTML+JS):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>
<div id="text">
<h1>AJAX jQuery.load()</h1>
<button onclick="loadTitle()">Load Title</button>
</div>
<script>
function loadTitle() {
$("#text").load("text.html");
}
</script>
</body>
</html>
only the function:
function loadTitle() {
$("#text").load("text.html");
}
