Take href of an anchor in a div and apply it to an image?

前端 未结 2 1952
悲&欢浪女
悲&欢浪女 2021-01-28 05:29

I have a div with an image and a link in it.

Is it possible that on page load, I can somehow find the href of the link and apply that the anchor tag of the image?

2条回答
  •  一生所求
    2021-01-28 06:11

    Still making assumptions about positioning of the elements, but the basics are below. Its very simple to do this in pure JS without needing to include jQuery.

    Live Demo

    var link = document.querySelectorAll('.card-prod a');
    link[0].href = link[1].href;
    

提交回复
热议问题