2013-05-13 73 views
2

嘿,我不是很流利的JavaScript和jQuery。 我想在一個div上單擊圖像替換div,並使圖像可點擊,即我想添加鏈接到該圖像。如何使JavaScript創建圖像的鏈接

爲了得到這個,我使用JavaScript

function ReplaceContentInContainer(id,content) {  

     var container = document.getElementById(id); 
     container.innerHTML = content; 
} 

我打電話從我index.ctp文件這個JavaScript作爲

<div id="vedio-image"> 
        </div> 

     <div class="title"> 
             <a href="javascript:ReplaceContentInContainer('vedio-image', 
              '<img width=\'480px\' height=\'220px\' src=\'<?php echo $this->webroot.'img/'.$count['News']['videoImage']; ?> \'/ >')"> 
              <?php echo $count['News']['title'];?> 
             </a> 
            </div> 

ReplaceContentInContainer工作完美,但我不能夠做出圖像可點擊。 請任何人都可以幫忙。

+2

您沒有在''標籤中寫'onclick',或者不在''標籤上寫''標籤 – Raptor 2013-05-13 01:29:55

+0

另外,您沒有使用jQuery。 – Jace 2013-05-13 01:30:21

+0

現在這被認爲是一個問題? :) – 2013-05-13 01:31:10

回答

1

你沒有在<img>標籤寫入onclick,或者不環繞你<img>標籤與標籤<a>

更改HTML代碼:

<div id="vedio-image"></div> 
<div class="title"> 
    <a href="javascript:ReplaceContentInContainer('vedio-image', 
              '<a href=\'http://example.com\'><img width=\'480px\' height=\'220px\' border=\'0\' src=\'<?php echo $this->webroot.'img/'.$count['News']['videoImage']; ?> \' /></a>')"> 
    <?php echo $count['News']['title'];?></a> 
</div> 

旁註:我加border="0"避免出現在IE中的藍色邊框。

+0

非常感謝Shivan Raptor,因爲我告訴我不擅長JavaScript ....感謝很多人的幫助 – winnie 2013-05-13 02:05:33