I have 4 squares (divs) And 1 large div where I want the content to be loaded in according to which div is clicked
Kind of like 'the technology' section of http://www.w3.org/html/logo/
How can I do that using javascript/jquery?
First I have to say that I'm really new to jQuery.
EDIT: this is the fixed version for future reference
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
.square{
width: 100px;
height: 100px;
float: left;
background: #CCC;
}
#details{
width: 400px;
height: 100px;
float: left;
background: #999;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.square:first').css('background-color', 'red');
$(".square").on("click", function() {
$('.square').css('background-color', ' #ccc');
$(this).css('background-color','red');
var id= 'Content for div ' + $(this).attr("contentId");
$('#details').fadeOut(function(){
$(this).text(id).fadeIn();
})
});
});
</script>
</head>
<body>
<div class="square" contentId="c1"></div>
<div class="square" contentId="c2"></div>
<div class="square" contentId="c3"></div>
<div class="square" contentId="c4"></div>
<div id="details"></div>
<div style="display: none" id="c1"> Content for div 1</div>
<div style="display: none" id="c2"> Content for div 2</div>
<div style="display: none" id="c3"> Content for div 3</div>
<div style="display: none" id="c4"> Content for div 4</div>
</body>
</html>
<div class="square" contentId="c1"></div>
<div class="square" contentId="c2"></div>
<div class="square" contentId="c3"></div>
<div class="square" contentId="c4"></div>
<div id="details"></div>
<div style="display: none" id="c1"> Content for div 1</div>
<div style="display: none" id="c2"> Content for div 2</div>
<div style="display: none" id="c3"> Content for div 3</div>
<div style="display: none" id="c4"> Content for div 4</div>
<script type="text/javascript">
$(".square").on("click", function() {
var id= $(this).attr("contentId");
$("#details").html($("#" + id).html());
});
</script>
Here's a simple example. HTML:
<div id='target'>Click something below.</div>
<div class='x'>Badda</div>
<div class='x'>Bing</div>
<div class='x'>Badda</div>
<div class='x'>Boom</div>
And then using jQuery:
$('.x').click(function() { $('#target').html($(this).html()); });
Or you could be a bit more adventurous and do it by decorating your divs with an attribute describing where they should be duplicated to on click.
<div id='target'>Click something below.</div>
<div dup_to='target'>Badda</div>
<div dup_to='target'>Bing</div>
<div dup_to='target'>Badda</div>
<div dup_to='target'>Boom</div>
Then...(no jQuery here but it needs querySelectorAll, you could do it with a simple loop or class name as well):
elems = document.querySelectorAll('[dup_to]');
for (i = 0; i < elems.length; ++i)
{
elems[i].onclick = dup_content_to(elems[i].getAttribute('dup_to'));
}
function dup_content_to(target)
{
return function() { document.getElementById(target).innerHTML = this.innerHTML; };
}
(Hurrah for over-engineering.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.