如何获取URL的一部分并将其打印在“src”属性

时间:2017-06-10 22:59:58

标签: javascript jquery ajax

我的网址= http://jafkz.com/index.php#23

我的代码:(阅读JS评论,如何制作第2步和第3步?)

 <script>

function mdlbox() {
     //step 1: Show modal box
  var y = document.getElementsByClassName('modalDialog');
        y[0].style.display = 'block';

    //step 2: Get hash value from My URL

    //step 3: Add this value to src attribute in My Div 
    }
    </script>

我的Div:

<div id="videobox">
                    <video>

                    <source src="videos/" type="video/mp4">
                    </video>
 </div>

如何使src看起来像src="videos/23"

3 个答案:

答案 0 :(得分:0)

使用document.querySelector('#videobox > video > source').src设置其网址

&#13;
&#13;
function mdlbox() {
     //step 1: Show modal box
  //var y = document.getElementsByClassName('modalDialog');
        //y[0].style.display = 'block';

    //step 2: Get hash value from My URL

    //step 3: Add this value to src attribute in My Div 
    document.querySelector('#videobox > video > source').src="https://www.w3schools.com/html/movie.mp4";//change it as "videos/23" in your case
    }
    mdlbox();
&#13;
<div id="videobox">
                    <video controls autoplay>

                    <source src="videos/" type="video/mp4">
                    </video>
 </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果要提取当前网址中#后面的值。 (在你的情况下&#39; 23&#39;)你可以这样做:

//step 2: Get` hash value from My URL
var hasValue = window.location.hash.substr(1);

答案 2 :(得分:0)

第2步:从我的网址

获取哈希值

var myHash = window.location.hash.substr(1);

步骤3:将此值添加到My Div中的src属性

document.querySelector('#videobox&gt; video&gt; source')。src ='videos /'+ myHash;

第4步:重新加载和播放视频

document.querySelector('#videobox&gt; video')。load();

document.querySelector('#videobox&gt; video')。play();