2016-01-28 88 views
1

我想創建一個名稱,類和ID的可點擊的圖像,如果我點擊該圖像,它應該顯示該圖像的ID,而不使用數據庫或post.For例如:屬性在我的價值

<div class='widget-content' style="min-height: 300px;"> 
     <img src="images/icons/running.jpg" 
     id="runningDrop" 
     class="droplink" /> 
     <img src="images/icons/swimming.jpg" 
     id="swimmingDrop" 
     class="droplink" /> 
</div> 

如果我單擊其中一個圖像,它應顯示編號爲swimmingDroprunningDrop

+1

當你標記的jQuery這是jQuery的解決方案$( 'IMG')。在( '點擊',函數(){警報($(本).attr( 'ID') )}); –

回答

0

簡單onclick="alert(this.id)"

<div class='widget-content' style="min-height: 300px;"> 
 
     <img src="images/icons/running.jpg" 
 
     id="runningDrop" 
 
     class="droplink" 
 
     onclick="alert(this.id)"/> 
 
     <img src="images/icons/swimming.jpg" 
 
     id="swimmingDrop" 
 
     class="droplink" 
 
     onclick="alert(this.id)"/> 
 
</div>

,正如我在評論說,雖然你標記的jQuery這是jQuery的解決方案

$('img').on('click', function(){ alert($(this).attr('id')) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class='widget-content' style="min-height: 300px;"> 
 
     <img src="images/icons/running.jpg" 
 
     id="runningDrop" 
 
     class="droplink"/> 
 
     <img src="images/icons/swimming.jpg" 
 
     id="swimmingDrop" 
 
     class="droplink"/> 
 
</div>

+0

謝謝!這真的幫助我,我希望我能成爲像你一樣guy'z –

+0

@DitNik 1st歡迎第2我推薦https://www.youtube.com/watch?v=GNb8T5NBdQg&list=PL6B08BAA57B5C7810學習jquery .. 3rd好運:) –

+0

我想知道你能教我如何將它分配給我的表單中的特定輸入,因爲它的值 –

0

我建議使用jQuery,因爲這將讓生活更輕鬆:

$('img').on({ 
    click: function(){ 
     alert($(this).attr("id")); 
    } 
}) 
+0

感謝它真的幫了我很多,我嘗試瞭解更多關於jquery的知識! –

0
<html> 
    <head> 
     <title>sample Page</title> 
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script> 

    <script> 

     $(document).ready(function() { 

      $('img').click(function() { 
       alert($(this).attr("id")); 
      }); 

     }); 


    </script> 
    </head> 
    <body> 
    <div class='widget-content' style="min-height: 300px;"> 
     <img src="images/icons/running.jpg" 
     id="runningDrop" 
     class="droplink" /> 
     <img src="images/icons/swimming.jpg" 
     id="swimmingDrop" 
     class="droplink" /> 
</div> 
    </body> 
    </html>