从多个切换和加载一个选项卡

时间:2017-04-14 16:56:11

标签: javascript jquery ajax twitter-bootstrap

如何使用Javascript或JQuery设置代码,每次只加载一个标签,在此代码中所有标签都一次加载,同时占用太多内存,特别是当故事长于2000字母,所以有三个部分,页面会同时加载6000个字母,我只想加载一个部分,而当我切换到另一个标签时,前一个标签将消失,只显示仅有2000个字母的新标签



<head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    
    </head>
    
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item">
            <a class="nav-link active" data-toggle="tab" href="#part1" role="tab">Part 1</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#part2" role="tab">Part 2</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#part3" role="tab">Part 3</a>
        </li>
    </ul>
    
    <!-- Tab panes -->
    <div class="tab-content">
        <div class="tab-pane active" id="part1" role="tabpanel"><b>Story Part 1</b> <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione aliquam ab esse amet quam, alias commodi laboriosam nemo voluptates. Libero fuga, suscipit eos rem voluptatibus. Itaque sequi quaerat quidem doloribus. <br> BGM: <br><audio controls>
      <source src="https://freemusicarchive.org/music/listen/156afda13aef642c35b844c21681ad44002b8e88" type="audio/mpeg">
    </audio>
        </div>
        <div class="tab-pane" id="part2" role="tabpanel"><b>Story Part 2</b> <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione aliquam ab esse amet quam, alias commodi laboriosam nemo voluptates. Libero fuga, suscipit eos rem voluptatibus. Itaque sequi quaerat quidem doloribus. <br> BGM: <br><audio controls>
      <source src="https://freemusicarchive.org/music/listen/156afda13aef642c35b844c21681ad44002b8e88" type="audio/mpeg">
    </audio></div>
        <div class="tab-pane" id="part3" role="tabpanel"><b>Story Part 3</b> <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione aliquam ab esse amet quam, alias commodi laboriosam nemo voluptates. Libero fuga, suscipit eos rem voluptatibus. Itaque sequi quaerat quidem doloribus. <br> BGM: <br><audio controls>
      <source src="https://freemusicarchive.org/music/listen/156afda13aef642c35b844c21681ad44002b8e88" type="audio/mpeg">
    </audio></div>
    </div>
&#13;
&#13;
&#13;

我的数据库是

[ Title  --     Part1    --     Part2   --     Part3  ]
[Part 1  --  LoremIpusem -- LoremIpusem -- LoremIpusem]
[Part 2  --  LoremIpusem -- LoremIpusem -- LoremIpusem]
[Part 3  --  LoremIpusem -- LoremIpusem -- LoremIpusem]

请给我一个关于如何至少在两个标签上执行此操作的示例,我已经检查了这个code但我意识到如果它被加载一次,它会保持加载

1 个答案:

答案 0 :(得分:1)

您可以添加占位符data-src,当用户点击标签切换时,src:

<audio id="sound1" preload="auto" data-src="http://www.jezra.net/audio/skye_boat_song.ogg" controls></audio>

$(".nav-tabs a").click( function () {
    var tab = $(this).attr('href');
    var $el = $(tab).find('audio');
    var src = $el.data('src');
    $el.attr('src', src);
})

https://jsfiddle.net/vtu8pz6y/

如果你想动态播放,暂停等等:

   $el[0].play()
   $el[0].pause()

More info here