我想通过点击图像来替换另一个div的内容(使用css隐藏)的div的内容。然而,使用这里建议的几个方法,我似乎无法得到它的工作。用另一个div的内容替换div的内容jQuery
我的代码如下:
HTML
<h3>Recent Callaborations</h3>
<div id="collaborations">
<img class="rec1" src="http://domain.com/michaelscott/wp-content/uploads/2013/02/url.png"/>
<div id="rec1">Some image caption that is hidden now and that will replace what is in the rec div below</div>
</div>
<div id="rec-box" class="rec">
this is the content that is to be replaced
</div>
JS
jQuery(document).ready(function() {
jQuery(".rec1").click(function(event) {
event.preventDefault()
jQuery('#rec-box').html($(this).next('#rec1')[0].outerHTML);
});
});
CSS
#collaborations {
width: 100%;
margin:0 0 10px 0;
padding:0;
}
#collaborations img {
display:inline-block;
}
.rec {
background: #EDEDED;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
padding: 10px;
}
#rec1, #rec2, #rec3, #rec4 {
display:none;
}
P.S.后
.append()
它$('#rect-box')
''''是'jQuery'的简写形式,通常更容易阅读,所以请使用它,除非你有命名冲突(即除非其他库已经使用'$') – nbrooks是的我认为我有冲突,因为只有jQuery作品 – dodgerogers747