admin管理员组文章数量:1203973
Here's my HTML:
<div class="large">
<img src="/images/photos/Interior.jpg" alt="The interior" style="[...]" />
<div class="caption">The interior</div>
</div>
<div class="small">
<img src="/images/photos/Bedroom.jpg" alt="Bedroom" style="[A different ...]" />
<div class="caption">A bedroom</div>
</div>
Upon clicking a div.small
, I'd like both the images and captions to swap container divs. The catch is I can't just swap the src, as there are a bunch of inline styles set which need to be preserved. Finally, once the images have been swapped, I want to apply my custom function .fitToParent()
to both of them.
How would I go about doing this?
Here's my HTML:
<div class="large">
<img src="/images/photos/Interior.jpg" alt="The interior" style="[...]" />
<div class="caption">The interior</div>
</div>
<div class="small">
<img src="/images/photos/Bedroom.jpg" alt="Bedroom" style="[A different ...]" />
<div class="caption">A bedroom</div>
</div>
Upon clicking a div.small
, I'd like both the images and captions to swap container divs. The catch is I can't just swap the src, as there are a bunch of inline styles set which need to be preserved. Finally, once the images have been swapped, I want to apply my custom function .fitToParent()
to both of them.
How would I go about doing this?
Share Improve this question asked Sep 4, 2009 at 18:24 EricEric 97.6k54 gold badges254 silver badges388 bronze badges 1- 8 Lol rofl etc. !! I just found myself looking for a good way to swap div contents using jQuery. And look who answered this question..... – Ropstah Commented Mar 19, 2010 at 16:18
3 Answers
Reset to default 16$(document).ready(function() {
$('div.small').click(function() {
var bigHtml = $('div.large').html();
var smallHtml = $(this).html();
$('div.large').html(smallHtml);
$('div.small').html(bigHtml);
//custom functions?
});
});
function swapContent(){
var tempContent = $("div.large").html();
$("div.large").empty().html($("div.small").html());
$("div.small").empty().html(tempContent);
}
<div class="large">
<img src="/images/photos/Interior.jpg" alt="The interior" style="[...]" />
<div class="caption">The interior</div>
</div>
<div class="small" onclick="swapContent()">
<img src="/images/photos/Bedroom.jpg" alt="Bedroom" style="[A different ...]" />
<div class="caption">A bedroom</div>
</div>
Hope it helps.
Change your markup a little:
<div id="large">
<div id="small">
And then in the Javascript you can do:
var swap = function(fromId, toId){
var temp = $(toId).html();
$(toId).html($(fromId).html());
$(fromId).html(temp);
}
Obviously it can be cleaned up, but you get the idea.
本文标签: javascriptSwapping div contents with jQueryStack Overflow
版权声明:本文标题:javascript - Swapping div contents with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738659655a2105345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论