English 中文(简体)
如何从目录中获取所有文件, 然后将其变成 Javascript 函数?
原标题:How to get all files from a directory and then make them into a javascript function?

对于一个我现在正在研究的项目, 我必须手动添加函数来播放这样的歌曲

function song(){
    mySong.src="song1.mp3";
    mySong.play();
    document.getElementById("p2").innerHTML="Now Playing:Song1";
}

但我想能够用 php 搜索目录, 从 php 生成这些函数, 这样我就不用手工添加每首歌了。 这是我的测试代码, 我知道我做错了, 但我似乎搞错了!

<?php
Header("content-type: application/x-javascript");
if ($handle = opendir( /wamp/www/songs/ )) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo  function $entry(){mySong.src=$entry; mySong.play();       
            document.getElementById("p2").innerHTML="Now Playing:" $entry;} ;
            echo "$entry";
        }
    }
    closedir($handle);
}
?>
问题回答

< strik> 在您的页面中定义 JUS 函数, 并给歌词源设定参数 :

function song(entry){
    mySong.src=entry;
    mySong.play();
    document.getElementById("p2").innerHTML="Now Playing: " + entry;
}

In your PHP, you ll most likely want to echo a link to play each of the songs inside that folder, right?
Do this:

<?php
//Header("content-type: application/x-javascript");
if ($handle = opendir( /wamp/www/songs/ )) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo  <a href="javascript:void(0)" onclick="song(   . $entry .   )">  . $entry .  </a><br /> ;
        }
    }
    closedir($handle);
}
?>

请注意, 这将用扩展名显示文件路径名称, 更好地显示您 d 只需要用 php/ js < code> substr / code > substring explode / split 进行一些字符串操作 。

好吧,我重写代码 来配合你的特例:

<强 > JS (头)

function song(entry){
    var mySong=document.getElementById("song1");
    mySong.src=entry;
    mySong.play();
    document.getElementById("p2").innerHTML="Now Playing: " + entry;
}

<强 > PHP+ HTML (体)

<?php
//Header("content-type: application/x-javascript");
$path =  /wamp/www/songs/ ;
if ($handle = opendir($path)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo  <a href="javascript:void(0)" onclick="song(   . $path . $entry .   )">  . $entry .  </a><br /> ;
        }
    }
    closedir($handle);
}
?>
<p id="p2"></p>
<audio id="song1" controls="controls">
    <source src="" type="audio/mpeg" />
</audio>

这将对 HTML5 支持的浏览器中播放的 mp3 文件有效 。

请注意 Firefox 不支持. mp3 回放 标签, 见: < a href=" https://stackoverflow.com/ questions/ 4923136/ whis- doest-firefox- support- mp3- file-format- in- audio > 为何 Firefox 不支持 < audio> 中的 MP3 文件格式?

因此,这个解决方案( 使用您的 < code\ lt; audio> ) 只会在铬中产生满意的结果( 未在歌剧和外观上测试) 。 您可能想要从另一个解决方案返回到另一个兼容性更高的解决方案 。

如果您想要与 IE 和 Firefox 兼容, 您可以尝试使用 :

<强 > JS

function song(entry){
    var mySong=document.createElement( embed );
    mySong.src=entry;
    mySong.width= 250px ;
    mySong.height= 60px ;
    document.getElementById("song1").innerHTML=   ;
    document.getElementById("song1").appendChild(mySong);
    document.getElementById("p2").innerHTML="Now Playing: " + entry;
}

<强 > PHP+ HTML

<?php
//Header("content-type: application/x-javascript");
$path =  /wamp/www/songs/ ;
if ($handle = opendir($path)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo  <a href="javascript:void(0)" onclick="song(   . $path . $entry .   )">  . $entry .  </a><br /> ;
        }
    }
    closedir($handle);
}
?>
<p id="p2"></p>
<div id="song1"></div>

这将有效地交叉浏览, 但可能要求安装 Firefox 插件 。

您对您的引号有问题。 单引号不分析变量。 另外, 您在错误的地方有一些引号 。

<?php
Header("content-type: application/x-javascript");
if ($handle = opendir( /wamp/www/songs/ )) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
          echo "function $entry(){mySong.src= $entry ;mySong.play();document.getElementById( p2 ).innerHTML= Now Playing: $entry ;}";
        }
    }
    closedir($handle);
}
?>

但你会对此有另一个问题... 你的歌曲可能不符合正确的功能名称... 你将不得不重新思考这个"每首歌的功能"...





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.