How to target the href to div

前端 未结 7 2031
不知归路
不知归路 2020-12-14 02:58

Here i am trying to open and get the contents of one div to target div on-click on a href. Here i have table where i have hrefs which has the link to div ids, and i have an

相关标签:
7条回答
  • 2020-12-14 03:35

    Put for div same name as in href target.

    ex: <div name="link"> and <a href="#link">

    0 讨论(0)
  • 2020-12-14 03:37

    havent tried but this might help

    $(document).ready(function(){
    r=0;s=-1;
    $(a).click(function(){
    v=$(this).html();
    $(a).each(function(){
    if($(this).html()==v)
    return;
    else ++r;
    $(div).each(function(){
    if(s==r)
    $(div).appendTo($(".target")); 
    ++S;
    });
    
    });
    
    });
    

    });

    0 讨论(0)
  • 2020-12-14 03:40

    if you use

    angularjs

    you have just to write the right css in order to frame you div html code

    <div 
    style="height:51px;width:111px;margin-left:203px;" 
    ng-click="nextDetail()">
    </div>
    


    JS Code(in your controller):

    $scope.nextDetail = function()
    {
    ....
    }
    
    0 讨论(0)
  • 2020-12-14 03:43

    You can put all your #m1...#m9 divs into .target and display them based on fragment identifier (hash) using :target pseudo-class. It doesn't move the contents between divs, but I think the effect is close to what you wanted to achieve.

    Fiddle

    HTML

    <div class="target">
        <div id="m1">
            dasdasdasd m1
        </div>
        <!-- etc... -->
        <div id="m9">
            dasdasdsgaswa m9
        </div>   
    </div>
    

    CSS

    .target {
        width:50%;
        height:200px;
        border:solid black 1px; 
    }
    .target > div {
        display:none;
    }
    
    .target > div:target{
        display:block;
    }
    
    0 讨论(0)
  • 2020-12-14 03:48

    From what I know this will not be possible only with css. Heres a solution how you could make it work with jQuery which is a javascript Library. More about jquery here: http://jquery.com/

    Here is a working example : http://jsfiddle.net/uyDbL/

    $(document).ready(function(){
        $('a').on('click',function(){
            var aID = $(this).attr('href');
            var elem = $(''+aID).html();
    
            $('.target').html(elem);
        });
    });
    

    Update 2018 (as this still gets upvoted) here is a plain javascript solution without jQuery

    var target = document.querySelector('.target');
    [...document.querySelectorAll('table a')].forEach(function(element){
        element.addEventListener('click', function(){
            target.innerHTML = document.querySelector(element.getAttribute('href')).innerHTML;
        });
    });
    a{
        text-decoration:none;
        color:black;
    }
    
    .target{
        width:50%;
        height:200px;
        border:solid black 1px; 
    }
    
    #m1, #m2, #m3, #m4, #m5, #m6, #m7, #m8, #m9{
        display:none;
    }
    <table border="0">
    <tr>
    <td>
    <hr>
    <a href="#m1">fea1</a><br><hr>
    <a href="#m2">fea2</a><br><hr>
    <a href="#m3">fea3</a><br><hr>
    <a href="#m4">fea4</a><br><hr>
    <a href="#m5">fea5</a><br><hr>
    <a href="#m6">fea6</a><br><hr>
    <a href="#m7">fea7</a><br><hr>
    <a href="#m8">fea8</a><br><hr>
    <a href="#m9">fea9</a>
    <hr>
    </td>
    </tr>
    </table>
    
    
    <div class="target">
    
    </div>
    
    
    <div id="m1">dasdasdasd</div>
    <div id="m2">dadasdasdasd</div>
    <div id="m3">sdasdasds</div>
    <div id="m4">dasdasdsad</div>
    <div id="m5">dasdasd</div>
    <div id="m6">asdasdad</div>
    <div id="m7">asdasda</div>
    <div id="m8">dasdasd</div>
    <div id="m9">dasdasdsgaswa</div>

    0 讨论(0)
  • 2020-12-14 03:49

    easy way to do that is like

    <a href="demo.html#divid">Demo</a>
    
    0 讨论(0)
提交回复
热议问题