根据选择选项jQuery WordPress显示-隐藏

时间:2016-10-03 作者:DopeAt

我是Wordpress的新手,我有一个大问题。。我想在特定页面中创建基于选择选项的显示隐藏,如下所示

http://jsfiddle.net/FvMYz/

但当我把我的代码,我没有工作。。。有选项列表,但不起作用。。。我应该在函数中安装任何脚本吗。php?或者我应该使用任何插件使其工作。。。提前谢谢你

这是我的密码员

https://jsfiddle.net/DopeAt/e63e3662/

        <select style="margin: 2% 0 3% 37.84%;" class="div-toggler" data-target=".Greekinfo">
          <option value="">Select game...</option>
<option value="agwn1" data-show=".agwn1">game 1</option>
<option value="agwn2" data-show=".agwn2">game 2</option>
<option value="agwn3" data-show=".agwn3">game 3</option>
<option value="agwn4" data-show=".agwn4">game 4</option>
<option value="agwn5" data-show=".agwn5">game 5</option>
<option value="agwn6" data-show=".agwn6">game 6</option>
<option value="agwn7" data-show=".agwn7">game 7</option>
<option value="agwn8" data-show=".agwn8">game 8</option>
<option value="agwn9" data-show=".agwn9">game 9</option>
<option value="agwn10" data-show=".agwn10">game 10</option>
<option value="agwn11" data-show=".agwn11">game 11</option>
<option value="agwn12" data-show=".agwn12">game 12</option>
<option value="agwn13" data-show=".agwn13">game 13</option>
<option value="agwn14" data-show=".agwn14">game 14</option>
<option value="agwn15" data-show=".agwn15">game 15</option>
<option value="agwn16" data-show=".agwn16">game 16</option>
<option value="agwn17" data-show=".agwn17">game 17</option>
<option value="agwn18" data-show=".agwn18">game 18</option>
<option value="agwn19" data-show=".agwn19">game 19</option>
<option value="agwn20" data-show=".agwn20">game 20</option>
<option value="agwn21" data-show=".agwn21">game 21</option>
<option value="agwn22" data-show=".agwn22">game 22</option>
<option value="agwn23" data-show=".agwn23">game 23</option>
<option value="agwn24" data-show=".agwn24">game 24</option>
<option value="agwn25" data-show=".agwn25">game 25</option>
<option value="agwn26" data-show=".agwn26">game 26</option>
<option value="agwn27" data-show=".agwn27">game 27</option>
<option value="agwn28" data-show=".agwn28">game 28</option>
<option value="agwn29" data-show=".agwn29">game 29</option>
<option value="agwn30" data-show=".agwn30">game 30</option>

    <div class="Greekinfo">
<div class="agwn1">
   <table style="margin-top: 10%;" class="table table-bordered">
  <thead>
<tr>
        <td>teamteamteamΑΕΚ</td>
        <td>teamteamΠαναιτωλικός</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΑΕΛ</td>
        <td>teamteamΒέροια</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΑ.Ο.Κέρκυρα</td>
        <td>teamteamΑτρόμητος Αθ.</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΑΕΠ Ηρακλής</td>
        <td>teamteamΑ.Ο Ξάνθη</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΛεβαδειακός</td>
        <td>teamteamΠΑΟΚ</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΟλυμπιακός</td>
        <td>teamteamΑστέρας Τρίπολης</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΠανιώνιος</td>
        <td>teamteamΠΑΣ Γιάννινα</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
    <tr>
        <td>teamteamΠλατανιάς</td>
        <td>teamteamΠαναθηναϊκός</td>
        <td>teamteam00/00/00</td>
        <td>teamteam00:00</td>
        <td>teamteamNova</td>
    </tr>
</tbody>

和我的JS代码

            <script>
       $(\'select.div-toggler\').change(function(){
           var target = $(this).data(\'target\');
           $(target).children().addClass(\'hide\');
         var show = $("option:selected", this).data(\'show\');
        $(show).removeClass(\'hide\');
               });
            </script>

1 个回复
最合适的回答,由SO网友:mrkoopie 整理而成

WordPress以无冲突模式运行jQuery。这意味着您要么必须使用jQuery而不是$,要么可以包装代码:

jQuery(function($) {

    //jQuery code

});
代码位于http://jsfiddle.net/FvMYz/3898/ 应该工作并使用jQuery的noConflict模式。看见http://jsfiddle.net/FvMYz/3897/ 对于允许您使用$的包装版本。两者都应该起作用。

在哪里保存代码

您应该永远不要将脚本存储在函数中。php。相反,创建assets/js/your脚本。js并将代码存储在主题文件夹中。在函数中添加以下内容。php:

function themeslug_enqueue_script() {
    wp_enqueue_script( \'my-js\', get_stylesheet_directory_uri().\'/assets/js/your-script.js\', array( \'jquery\' ) );
}

add_action( \'wp_enqueue_scripts\', \'themeslug_enqueue_script\' );
参考号:https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scriptshttps://developer.wordpress.org/reference/functions/wp_enqueue_script/