2014-03-07 56 views
0

我在網上隨處搜索以獲取此目標的答案。將腳本標記更改爲可點擊的圖像

<script class="custom" 
    src="myscript.js" 
    data-image="button.png" 
    data-product="000452"> 
</script> 

我想腳本標記加載,這將在頁面加載使用數據,圖像屬性值轉換腳本標籤本身可點擊圖片myscript.js,然後點擊時myscript.js將得到數據產品的價值,並將用戶轉發到mywebsite.com/product/{value}

這聽起來很簡單,但我迄今未能實現這一點,也有可能得到這個沒有jQuery或使用非常小JS庫?

非常感謝。

+2

這是我不清楚! –

+0

爲什麼不直接輸出圖像? – Andrew

+0

提供您的http://jsfiddle.net/ –

回答

1

我更換了圖像標籤的jQuery腳本,希望這對你的工作

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" class="custom" data-image="button.png" data-product="000452"></script> 


      <script type="text/javascript"> 
      $(document).ready(function(){ 


       $('.custom').after('<a href ="http://mywebsite.com/product/'+$('.custom').attr('data-product')+'"><img src='+$('.custom').attr('data-image')+' /></a>'); //add img tag after script tag 


       $('.custom').remove(); //remove the script tag 
      }); 
      </script> 
+0

這很有效,最後一件事,是否有一種方法可以在不使用jquery或使用非常輕的JS庫的情況下完成此操作? – Cindro

+0

不用擔心,我對此感到滿意。 – Cindro

相關問題