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

前端 未结 2 1951
悲&欢浪女
悲&欢浪女 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;
    
    0 讨论(0)
  • 2021-01-28 06:24

    try this:

    $(document).ready(function(){
    
        $("#theDiv").find("a").attr("href", $('img').attr('src'));
    
    });
    


    updated:

    $(document).ready(function(){
    
         $("#anchor").attr("href", $('#continue').attr('href'));
    
    });
    

    http://jsfiddle.net/fFgwb/5/


    so, you should add classes like .divs to divs and other classes to anchor links:

    $(document).ready(function(){
    
        $(".divs").each(function(){
    
            $(this).find("a").hasClass("anchor").attr("href", $(this).find("a").hasClass("continue").attr('href'))
    
         });
    
    });
    
    0 讨论(0)
提交回复
热议问题