【问题标题】:How can I consolidate my code? JavaScript and PHP based issues如何合并我的代码?基于 JavaScript 和 PHP 的问题
【发布时间】:2012-11-30 06:18:27
【问题描述】:

我的页面相当简单直接。我首先使用 DIV 标记将外部 PHP 页面作为我页面中的元素调用。然后我隐藏主页上不需要的 div,然后滑入查看想要的“页面”并隐藏不需要的。

我想做的是滑入想要的内容并滑出当前活动/可见的内容。我知道我可以继续我已经开始做的事情,但整个文档会很臃肿。有任何想法吗?谢谢!

    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

  <script>
  $(document).ready(function(e) {
$(function(){
    $("div.design, div.marketing").hide();
    });
});

  $(document).ready(function() {

$("h3.home_button").click(function () {
      $("div.home").show("slide", { direction: "left" }, 500);
      $("div.design").hide();
      $("div.marketing").hide();
});

$("h3.design_button").click(function () {
      $("div.design").show("slide", { direction: "left" }, 500);
      $("div.home").hide();
      $("div.marketing").hide();
});

$("h3.marketing_button").click(function () {
      $("div.marketing").show("slide", { direction: "left" }, 500);
      $("div.home").hide();
      $("div.design").hide();
});


  });
  </script>

</head>

<body>

<div class="main">

    <div class="header">
        <?php include("header.php");?>
    </div>

    <div class="menu">
        <?php include("menu.php");?>
    </div>

    <div class="content">
        <?php include("content_home.php");?>
        <?php include("content_design.php");?>
        <?php include("content_marketing.php");?>

    </div>
    <div class="footer"><?php include("footer.php");?></div>

</div>

【问题讨论】:

    标签: php javascript jquery effects modular


    【解决方案1】:

    您可以使用data attributes 并通过data() jquery 方法访问它。

    在您的 HTML 中

    <h3 data-a='marketing'>marketing</h3>
    <h3 data-a='design'>design</h3>
    <div class='container'>
        <div style='display:none;' class='marketing'>
             marketing stuffs
        </div>
        <div class='design'>
             design stuffs
        </div>
    </div>​
    

    在你的 javascript 中

    $('h3').click(function(){
        type = $(this).data('a');
    
        $('.container div').hide();
        $('.container').find('.'+type).show();
    })​
    

    这是一个例子fiddle

    你也调用了两次 ready 方法

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 2015-10-19
      • 2020-06-13
      • 2012-03-31
      • 2016-03-18
      • 2016-08-28
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多