如何通过 Javascript 在此 HTML 中传递 src 值

我已将代码粘贴到

    <https://pastebin.com/z8q5cavm>

我将在数据表中有大量行列表,每一行都有一个特定的 href 或 mp3 文件链接。然而,代码总是打开 serm.mp3,即使我在链接中传递了任何内容。

有人可以帮我解决这个javascript吗?


陪伴而非守候
浏览 219回答 1
1回答

一只名叫tom的猫

由于您已将 src 值硬编码为 serm.mp3,因此您得到了它。您需要添加以下脚本,以使用您发送到模态的数据值。&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $('#exampleModal').on('show.bs.modal', function(e) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var value = e.relatedTarget.dataset.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('audio').attr('src', value);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>这是演示。<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>Test</title>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <link rel='manifest' href='/manifest.json'>&nbsp; &nbsp; <link rel="stylesheet" src="style.css">&nbsp; &nbsp; <link rel="icon" type="image/png" href="icon1.png" />&nbsp; &nbsp; <link rel='manifest' href='/manifest.json'>&nbsp; &nbsp; <meta name="theme-color" content="#ffffff">&nbsp; &nbsp; <meta name="apple-mobile-web-app-status-bar" content="#ffffff" >&nbsp; &nbsp; &nbsp;<meta name="viewport" content="width=device-width, initial-scale=1.0"><!--mobile icons--><link rel='shortcut icon' type='image/x-icon' href="images/icon.png" /><link rel="apple-touch-icon" sizes="74x74" href="images/icon-74.png" /><link rel="apple-touch-icon" sizes="96x96" href="images/icon-96.png" /><link rel="apple-touch-icon" sizes="144x144" href="images/icon-144.png" /><link rel="apple-touch-icon" sizes="384x384" href="images/icon-384.png" /><link rel="apple-touch-icon" sizes="512x512" href="images/icon-512.png" />&nbsp; <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700|Oxygen:400,700" rel="stylesheet">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ekko-lightbox/5.3.0/ekko-lightbox.css">&nbsp; &nbsp; <link rel="stylesheet" href="css/animate.css">&nbsp; &nbsp; <link rel="stylesheet" href="css/owl.carousel.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="css/jquery.fancybox.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="fonts/ionicons/css/ionicons.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="fonts/fontawesome/css/font-awesome.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">&nbsp; &nbsp; <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>&nbsp; &nbsp; <!-- Theme Style -->&nbsp; &nbsp; <link href="http://vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">&nbsp; <script src="http://vjs.zencdn.net/4.12/video.js"></script>&nbsp; &nbsp; <link rel="stylesheet" href="css/style.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="../../extensions/Editor/css/editor.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.6.2/css/buttons.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.datatables.net/colreorder/1.5.2/css/colReorder.dataTables.min.css">&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="DataTables/datatables.min.css"/>&nbsp;&nbsp; &nbsp; <script type="text/javascript" src="DataTables/datatables.min.js"></script>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; a.buttons-collection {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; margin-left: 1em;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; </style>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; <section>&nbsp; &nbsp; <div style="margin: 20px;">&nbsp; &nbsp; &nbsp; <table id="example" class="display" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>S No</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Title</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Listen</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Download</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td >Mp31</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td ><a href="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3" title="Click To Listen"&nbsp; data-toggle="modal" data-target="#exampleModal"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data-value="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3">Listen <svg class="bi bi-caret-right-square-fill"&nbsp; width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <path fill-rule="evenodd" d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4v8z"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </svg> </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td >Mp32</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td ><a href="media/mp3/test2.mp3" title="Click To Listen"&nbsp; data-toggle="modal" data-target="#exampleModal" onClick=""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data-value="media/mp3/test2.mp3">Listen <svg class="bi bi-caret-right-square-fill"&nbsp; width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <path fill-rule="evenodd" d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4v8z"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </svg> </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tfoot>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>S No</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Title</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Listen</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Download</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tfoot>&nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </div></section>&nbsp;&nbsp;&nbsp; &nbsp; <!--MP3 modal-->&nbsp; &nbsp; <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"&nbsp; >&nbsp; &nbsp; &nbsp; <!-- Scrollable modal -->&nbsp; &nbsp; &nbsp; <div class="modal-dialog modal-dialog-centered modal-xl">&nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4 class="modal-title" id="exampleModalLabel">MP3</h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-body" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row" style="padding: 10px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-primary">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <audio id="player" controls><source src="" type="audio/mpeg"></audio>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <!-- loader -->&nbsp; <div id="loader" class="show fullscreen"><svg class="circular" width="48px" height="48px"><circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/><circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#f4b214"/></svg></div>&nbsp;&nbsp;&nbsp; <script src="js/jquery-3.2.1.min.js"></script>&nbsp; <script src="js/popper.min.js"></script>&nbsp; <script src="js/bootstrap.min.js"></script>&nbsp; <script src="js/owl.carousel.min.js"></script>&nbsp; <script src="js/jquery.waypoints.min.js"></script>&nbsp; <script src="js/jquery.fancybox.min.js"></script>&nbsp; <script src="js/main.js"></script>&nbsp; <script src="js/main.js"></script>&nbsp;&nbsp; &nbsp; <script src="js/app.js"></script>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script>$(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; var t = $('#example').DataTable( {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "columnDefs": [ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "searchable": false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderable": false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "targets": 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "order": [[ 1, 'asc' ]],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dom: 'Bfrtip',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttons: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'copyHtml5',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'excelHtml5',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'csvHtml5',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'pdfHtml5'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responsive: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; t.on( 'order.dt search.dt', function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.innerHTML = i+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp; &nbsp; } ).draw();&nbsp; &nbsp; } );</script>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $('#exampleModal').on('show.bs.modal', function(e) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var value = e.relatedTarget.dataset.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('audio').attr('src', value);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <script src="https://code.jquery.com/jquery-3.5.1.js"></script>&nbsp; &nbsp; <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>&nbsp;&nbsp;&nbsp; &nbsp; <script src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>&nbsp; &nbsp; <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript