自动创建javascript代码


Automatically creating javascript code

如果你不明白我需要什么帮助,很抱歉,很难说。

我基本上是在编写一个论坛系统,在我的论坛系统中,我希望管理员能够通过控制面板创建一个新的类别,然而,这个问题是,在每个类别上,我都希望有箭头可以向上滑动框,但我如何添加一段自动创建唯一箭头的代码和一段JavaScript代码,上面写着"一旦单击箭头,滑动框就会向上"

编辑:
jsfiddle.net/UxJkA当你点击第二个时,第一个会出现,我知道如何通过更改类来手动修复,但我希望它也能自动更改每个箭头和框的类。

Javascript代码

        $(document).ready(function(){
    $("#a_main_content").show(); 
        $('.arrow').click(function () {
            $("#a_main_content").slideToggle();
        });

        $("#p_main_content").show();
        $('.p_arrow').click(function () {
            $("#p_main_content").slideToggle();
        });
});

HTML代码

     <div id="bottom_corners"></div>
     <!-- Main content box -->

     <!-- Category 1 -->
     <div id="p_content_header">
     Category
     <img src="images/arrow.png" class="p_arrow">
     </div>
     <div id="p_main_content">
        <table>
    <tr>
    <td class="forum_name_header" style="width:240px;">Forum Name</td>
    <td class="forum_name_header" style="width: 300px;">Last Post</td>
    </tr>
    <tr>
    <td style="width:240px;" class="forum_name">James first forum<br />
                          <i style="color: #a3a3a3;">Topics: 858, Messages: 8,485</i></td>
    <td style="width: 300px;" class="forum_name">Test 1<br />
                              by <b style="color: red;">Admin</b></td>
    </tr>
        <tr>
    <td style="width:240px;" class="forum_name">James second forum<br />
                          <i style="color: #a3a3a3;">Topics: 434, Messages: 6,534</i></td>
    <td style="width: 300px;" class="forum_name">Test 2<br />
                              by <b style="color: blue;">Mod</b></td>
    </tr>
        </table>
     </div>
     <div id="bottom_corners"></div>
     <!-- Category 1 -->
     <!-- Category 2 -->
         <div id="p_content_header">
     Category 2
     <img src="images/arrow.png" class="p_arrow">
     </div>
     <div id="p_main_content">
        <table>
    <tr>
    <td class="forum_name_header" style="width:240px;">Forum Name</td>
    <td class="forum_name_header" style="width: 300px;">Last Post</td>
    </tr>
    <tr>
    <td style="width:240px;" class="forum_name">James first forum<br />
                          <i style="color: #a3a3a3;">Topics: 858, Messages: 8,485</i></td>
    <td style="width: 300px;" class="forum_name">Test 3<br />
                              by <b style="color: green;">Donator</b></td>
    </tr>
        <tr>
    <td style="width:240px;" class="forum_name">James second forum<br />
                          <i style="color: #a3a3a3;">Topics: 434, Messages: 6,534</i></td>
    <td style="width: 300px;" class="forum_name">Test 4<br />
                              by <b style="color: grey;">Registerd User</b></td>
    </tr>
            <tr>
    <td style="width:240px;" class="forum_name">James second forum<br />
                          <i style="color: #a3a3a3;">Topics: 434, Messages: 6,534</i></td>
    <td style="width: 300px;" class="forum_name">Test 5<br />
                              by <b style="color: #333;"><strike>Banned user</strike></b></td>
    </tr>
        </table>

。。。我真的不确定我是否理解你的问题,但以下是我的最佳猜测:http://jsfiddle.net/BXzKC/

<div id="p_content_header">Category
<img src="images/arrow.png" class="arrow" data-content="first" />
</div>
<div class="content-container" data-content="first">
    <table> ... </table>
</div>
<div id="p_content_header">Category 2
    <img src="images/arrow.png" class="arrow" data-content="second" />
</div>
<div class="content-container" data-content="second">
    <table> ... </table>
</div>

所以现在每个"盒子"都有一个公共类和一些数据属性来标识它。每个箭头也有一个公共类和相同的数据属性。

$(".content-container").show();
$('.arrow').click(function (e) {
    var cat = $(e.currentTarget).data('content');
    $('.content-container[data-content="' + cat + '"]').slideToggle();
});

所以基本上,我们在第一行同时显示所有的"框";然后,单击任意箭头,我们读取数据内容,并使用它来确定要展开/折叠的框。

你仍然需要自己为这些数据想出一些值(方框的名称?一个简单的计数?你也可以忽略所有这些,只抓取包含箭头的div后面的方框,或者将它们包装在一起或其他什么),但这是另一个问题。