簡體   English   中英

切換Div的內容onclick

[英]Switch Content of Div onclick

我有兩個div,每個div里面都有一張圖片。 如果我單擊第二個div內的圖像,則希望該圖像成為第一個div的內容(如圖庫)。

請勿替換img標簽內的鏈接,這一點很重要,因此document.IMG1name.src = document.IMG2name.src是不可能的。

我用innerhtml嘗試過,但是不起作用:

<div id="container1">
<img src="blablabla">
</div>

<div id="container2"; onclick="document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="../funnycat.jpg">
</div>

的HTML

<div class="pricing_table" id="monthly">
<ul>
<li>Basic</li>
<li><a href="" class="buy_now">Subscribe Now</a></li>
</ul>
   <ul style="background-color:#CCCCCC;">
   <h2>Monthly Plans</h2><a class='plansSwitch' href='#yearly'>Click here to switch to the  "Yearly Plans"</a></ul>
  </div>
  <div class="pricing_table" id="yearly">
  <ul>
 <li>Basic</li>
 <li><a href="" class="buy_now">Subscribe Now</a></li>
 </ul>
 <ul style="background-color:#CCCCCC;">
 <h2>Yearly Plans</h2><a class='plansSwitch' href='#monthly'>Click here to switch to the       "Monthly Plans"</a></ul>
</div>

JS

var $plansHolders = $('#monthly, #yearly').hide();
$('#monthly').show();
$('.plansSwitch').click(function() {
var href = $(this).attr('href');
$plansHolders.hide();
$(href).show();
});

您只需要在這樣的onclick動作上添加javascript:

<div id="container1"></div>

<div id="container2" onclick="javascript:document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="http://i.imgur.com/0fS8dCsb.jpg"/>
</div>

例子

嗯...您的代碼運行良好,只需關​​閉img標簽並刪除分號即可:

<div id="container1">
    <img src="blablabla" />
</div>

<div id="container2" onclick="document.getElementById('container1').innerHTML=document.getElementById('container2').innerHTML"><img src="https://www.google.es/logos/doodles/2013/holiday-series-2013-3-4504416610680832-hp.jpg" />
</div>

在這里,您有一個演示: http : //jsfiddle.net/HLs86/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM