douna2917 2012-08-03 19:39
浏览 60

通过向URL添加代码来显示特定的div

I have this code as an example:

<html>
<head>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
function showonlyone(thechosenone) {
     $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show(200);
          }
          else {
               $(this).hide(600);
          }
     });
}
</script>

</head>

<body>

             <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
                <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
             </div>
             <div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #1</div>


             <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
                <a id="myHeader2" href="javascript:showonlyone('newboxes2');" >show this one only</a>
             </div>
             <div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #2</div>

             <div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
                <a id="myHeader3" href="javascript:showonlyone('newboxes3');" >show this one only</a>
             </div>
             <div class="newboxes" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #3</div>

</body>
</html>

On a different page, I want to have a link that will show Div #2 when the user loads the page by clicking that external link. I would presume that such thing would be possible by some type of adjustment to the URL? For instance: domain.com/page.php?=showdiv2

Any assistance is appreciate, my knowledge of coding is limited (especially with PHP) so please be detailed if you choose to help.

  • 写回答

5条回答 默认 最新

  • dpus81500574 2012-08-03 19:42
    关注

    There are a number of ways to accomplish what you want to do here. I will explain how you can do it with $_GET.

    You can control this with a $_GET variable (at the end of the URL, just add ?varName=value where varName is the name of a variable (this is arbitrary), and value is the value you want to assign it.

    Then in your PHP code you can access this variable by a) checking if it exists and b) performing logic based on its value:

    <?php
        $divToShow = isset($_GET['div']) ? $_GET['div'] : "";
    
        //logic here
    ?>
    

    So for example, if you want <div id="div_2"> to be displayed, you could plug this in to jQuery by writing:

    <script language="javascript" type="text/javascript">
        $("div#div_<?= $divToShow ?>").show();
        $("div:not(#div_<?= $divToShow ?>)").hide();
    </script>
    

    OR you could simply force PHP to only print out div # 2:

    <?php
    // set $divId to a value
    
    if ($divId == $divToShow) : ?>
        <div id="<?= $divId ?>">
            <!-- content here -->
        </div>
    <?php endif; ?>
    
    评论

报告相同问题?