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

前端 未结 2 1972
悲&欢浪女
悲&欢浪女 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: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'))
    
         });
    
    });
    

提交回复
热议问题