2010-04-12 4 views
2

다음 자바 스크립트를 사용하여 만든 이미지에 대한 링크를 추가하는 방법.자바 스크립트를 사용하여 만든 이미지 요소에 링크를 추가하는 방법 javascript

도움이나 답장을 보내 주셔서 감사합니다.

for(var i=0; i<images.length; i++) { 
    var t = document.createElement('IMG'); 
    t.setAttribute('src',images[i]); 
    t.setAttribute('border', 0); 
    t.setAttribute('width',imageWidth); 
    t.setAttribute('height',imageHeight); 
    t.style.position = 'absolute'; 
    t.style.visibility = 'hidden'; 

    el.appendChild(t); 
} 
+1

그렇지 않은 추가 HTML 문서에서'setAttribute'를 사용하는 것이 좋습니다. 이것이 IE에서 실패하는 많은 경우가 있습니다 (당신이 여기에 충돌 한 것은 아니지만). DOM Level 1 HTML 속성을 선호하십시오 :'t.src = images [i];'등. – bobince

답변

5

이 시도 :

for(var i=0; i<images.length; i++) 
{ 

    var t = document.createElement('IMG'); 
    var link = document.createElement('a'); // create the link 
    link.setAttribute('href', 'www.example.com'); // set link path 
    // link.href = "www.example.com"; //can be done this way too 

    t.setAttribute('src',images[i]); 
    t.setAttribute('border', 0); 
    t.setAttribute('width',imageWidth); 
    t.setAttribute('height',imageHeight); 
    t.style.position = 'absolute'; 
    t.style.visibility = 'hidden'; 

    link.appendChild(t); // append to link 
    el.appendChild(link); 
} 
0

이 같은 방식에 a 요소를 만듭니다. 이미지 대신 el에 추가하고 이미지를 추가하십시오.

먼저과 같이 ... 다음 앵커 요소를 만들고 여기에 img 요소를 추가 할 필요가
0

:

for(var i=0; i<images.length; i++) { 
    var a = document.createElement('a'); 
    a.href = "http://www.MYWEBSITE.com/" 
    var t = document.createElement('IMG'); 
    t.setAttribute('src',images[i]); 
    t.setAttribute('border', 0); 
    t.setAttribute('width',imageWidth); 
    t.setAttribute('height',imageHeight); 
    t.style.position = 'absolute'; 
    t.style.visibility = 'hidden'; 
    a.appendChild(t); 
    el.appendChild(a); 
} 

그런 다음 '엘'

매트에 앵커를

관련 문제