简体   繁体   中英

Why won't the Javascript in my script tags work?

I learning javascript, and practicing with event handlers. I've written the correct code (Im sure...) to make an mp3 play when a button (button1) is clicked.

I tried putting the code in the beginning. I've tested to see if I typed in the right path for the mp3. I've tried deleting the sort of irrelevent 2 lines of event handlers in the middle of the script. I don't know what is going on! Can someone help?

<head>
</head>

<body>
<div><button id="button1" onclick="playMusic()">MUSIC</button>
<span><button id="button2">VIDEOS</button></span>
<span><button id="button3">PICTURES</button></span></div>
<div><button id="button4">BRAIN GAMES</button>
<span><button id="button5"><a 
href="C:\Users\sared\Documents\Vegas\message.html">TALK TO FAMILY</a> 
</button></span>
<span><button id="button6">STORIES</button></span></div>


<script>

    var item = document.getElementById("button1");
    var music = new Audio("C:\Users\sared\Documents\baccara.mp3");

    item.addEventListener("click", playMusic, false);
    item.addEventListener("dblclick", stopMusic, false);

    function playMusic() {
       music.play();
    }

    function stopMusic() {
       music.stop();
    }

    </script>

You need to escape the backslashes - \\ will break your file paths. Change \\ to \\\\ , and it should work. Keep in mind that JavaScript can't normally access the local file system like this though.

<a href="C:\Users\\sared\\Documents\\Vegas\\message.html">TALK TO FAMILY</a> 

JS:

var music = new Audio("C:\Users\\sared\\Documents\\baccara.mp3");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM