2017-04-20 47 views
-3

我想要得到的attribute.I的價值屬性的價值,但我不能得到的價值獲取試圖通過</p> <p><code>jQuery('#galleryalbum img').attr('alt'); </code> 搶alt屬性

jQuery(function() { 
 
    jQuery(document).ready(function() { 
 
    // jQuery('#galleryalbum img').each(function(){ 
 
    title = jQuery('#galleryalbum img').attr('alt'); 
 
    console.log(title); 
 
    //});  
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<div id="galleryalbum" style="height: 100px; width: auto; position: relative;"> 
 
    <div id="1" style="float: none; list-style: none; position: absolute; width: 429px; z-index: 0; display: none;"> 
 
    <img typeof="foaf:Image" src="http://localhost/intranet/sites/default/files/styles/gallery_image/public/Vidhya%20R%20-%20Hi5.jpg?itok=LCNjfrSm" width="700" height="300" alt="" title="Vidhya R - As you look back with pride and a sense of achievement... Here's wishing you many more years of success and joy at Aspire! Happy 5!" 
 
     style="width: 100%;"> 
 
    </div> 
 
</div>

+2

您的代碼似乎工作,但你'ALT =「」'是空的,所以這就是爲什麼它不以'的console.log寫東西( )' –

回答

2

您的代碼正常,但您的alt爲空。

alert($('img').attr('alt'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<img src="https://i.stack.imgur.com/5OA0z.jpg?s=64&g=1" alt="test">

0

在HTML ALT是空的,我相信你希望看到什麼是在標題。 如果您在alt =「這裏有些內容」中提供了一些內容,您將在控制檯中獲得結果。 如果你想看到的title屬性的文字,然後用

jQuery(function() { 
     jQuery(document).ready(function() { 
     // jQuery('#galleryalbum img').each(function(){ 
      title = jQuery('#galleryalbum img').attr('title'); 
      console.log(title); 
     // });  
     }); 
    });