javascript - div.appendChild().appendChild() - Stack Overflow

Logically, I want to embed an image in a link in a div.I've successfully done this with either t

Logically, I want to embed an image in a link in a div. I've successfully done this with either the link or the image. I've even done it with the link and image in parallel (which is useless) for my purposes. But can't seem to have the image wrapped in a link inside a div.

var div = OpenLayers.Util.createDiv();
var img = OpenLayers.Util.createImage(null, null, null, null, null, null, null, delayDisplay);
img.className = "olAlphaImg";
img.alt = altText;

var link = document.createElement("a");
// link.setAttribute("href", "#");
link.href="#" + altText;
link.appendChild(img);

div.appendChild(link); 

OpenLayers.Util.modifyAlphaImageDiv(div, id, px, sz, imgURL, position, border, sizing, opacity, altText);

return div;

This is for an OpenLayers where I'm trying to ensure keyboard navigation. I just am not sure how to do this with javascript and all the examples only use one appendChild reference. I've tried with innerHTML() but I wasn't using strings so that didn't seem useful.

Logically, I want to embed an image in a link in a div. I've successfully done this with either the link or the image. I've even done it with the link and image in parallel (which is useless) for my purposes. But can't seem to have the image wrapped in a link inside a div.

var div = OpenLayers.Util.createDiv();
var img = OpenLayers.Util.createImage(null, null, null, null, null, null, null, delayDisplay);
img.className = "olAlphaImg";
img.alt = altText;

var link = document.createElement("a");
// link.setAttribute("href", "#");
link.href="#" + altText;
link.appendChild(img);

div.appendChild(link); 

OpenLayers.Util.modifyAlphaImageDiv(div, id, px, sz, imgURL, position, border, sizing, opacity, altText);

return div;

This is for an OpenLayers where I'm trying to ensure keyboard navigation. I just am not sure how to do this with javascript and all the examples only use one appendChild reference. I've tried with innerHTML() but I wasn't using strings so that didn't seem useful.

Share Improve this question asked Apr 5, 2012 at 12:52 Mike GiffordMike Gifford 6611 gold badge9 silver badges17 bronze badges 2
  • can you setup jsfiddle for this – Sandeep Manne Commented Apr 5, 2012 at 12:54
  • True.. I will try to do that next time. Oleg's answer below answered my question, but would have been better if I'd just done what you'd suggested first. – Mike Gifford Commented Apr 5, 2012 at 20:47
Add a ment  | 

1 Answer 1

Reset to default 5

What do you mean by

can't seem to have the image wrapped in a link inside a div

Do you experience any errors?

I might have misunderstood your question, but if not, your code should work.

Here's my example: a working jsfiddle that wraps an image into a link into some container.

var img = document.createElement('img');
img.src = 'https://www.google.fr/images/srpr/logo3w.png';

var anchor = document.createElement('a');
anchor.href = 'http://google.';

// Wrap image in the link (anchor):
anchor.appendChild(img);
// Insert the anchor into container:
document.getElementById('container').appendChild(anchor);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744358153a4570334.html

相关推荐

  • javascript - div.appendChild().appendChild() - Stack Overflow

    Logically, I want to embed an image in a link in a div.I've successfully done this with either t

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信