开发者

how to use javascript to add link to image element created using javascript

开发者 https://www.devze.com 2022-12-26 06:01 出处:网络
how to add link for the image created using following javascript开发者_如何转开发. thanks for anyhelp or replies.

how to add link for the image created using following javascript开发者_如何转开发.

thanks for any help or replies.

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);
}


Try this:

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);
}


You need to first create a anchor element then append the img element to it... like so:

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);
}

Then append the anchor to 'el'

Matt


Create an a element in the same fashion. Append it to el instead of the image and append the image to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消