我希望通过点击图片将一个 div 标签的内容替换为另一个被用 CSS 隐藏的 div 标签的内容。但是,无论我采用这里建议的一些方法,似乎都不能使它正常工作。
以下是我的代码:
HTML:
以下是我的代码:
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;
}
$
是jQuery
的简写,通常更易阅读,因此除非存在命名冲突(即除非其他库已经使用$
),请使用它。 - nbrooks