2014-09-25 1 views
0

jQuery는 어떻게 요소가 속성을 가져올 수 있는지 확인할 수 있습니까?요소가 속성을 가져 왔는지 확인하십시오.

예 :

<img class="photo_image"> 

src 속성이 없습니다.

내가 사용 :

if($(v).find('img.photo_image').length) { } 

을하지만 그것에게 <img>를 확인하지 것은 src 속성을 가지고 여부. 감사합니다. .

+1

'경우 ($ (V) .find ('IMG [SRC] .photo_image'). 길이) {}' – chiliNUT

+0

또는 당신이 사용할 수있는'.attr()'. – Goose

답변

0

attr 함수는 속성이 설정되지 않은 경우 undefined을 반환합니다.

if ((typeof $('img.photo_image').attr('src')) == 'undefined') { // src is not set 
0

가장 좋은 방법은 이것이다 : 당신이없는 요소를 원하는 경우

if ($("img[src]").length > 0) 

: 당신이 요소는 속성이 있는지 확인하려면

var attr = $('.photo_image').attr('src'); 

// For some browsers, `attr` is undefined; for others, 
// `attr` is false. Check for both. 
if (typeof attr !== typeof undefined && attr !== false) { 
    // ... 
} 
0

는 다음 코드를 사용 속성, 시도 :

if ($("img:not([src])").length > 0) 
0
<img id="photo_image" /> 


var image = $('body').find('img#photo_image'); 

if (image.length) { 
if (image.attr('src')) { 
    console.log('src is set');  
} 
else { 
    console.log('src is not set'); 
} 
} 

http://jsfiddle.net/1qyag2je/

관련 문제