我有以下代码:
<head>
<meta charset="UTF-8">
<title>Audio Home Page</title>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
</head>
<body>
<div class = "content">
<p id = "nameOfAudio"></p>
</div>
<p>Order will should be: Train Sound, Emergency Sound, Horror Sound, End Of Audio Files,<br/> without old</p>
<script>
$(document).ready( function(){
$.getJSON('http://localhost:8181/setCookie.php', function(data){
"use strict";
var items = [];
$.each( data, function( key, val ) {
items.push( val );
});
var newElem = create(items);
$(".content").html(newElem);
});
function create(audioSRC) {
output = '<audio id="audioElement">';
output += '<source src=audio/'+audioSRC[0]+' type="audio/wav" />';
output += '</audio>';
return output;
}
});
</script>
</body>
我的问题是我想向功能<div>
甚至<p>
添加从功能创建的新音频元素。但这根本不起作用。
我从 ajax方法jquery 尝试没有结果。
jQuery方法html,append和document.getElementById.innerHTML
不起作用。我对函数返回的代码(对于新元素)发出了警告,这没有问题。
在.getJSON
,.ajax
中设置新内容是否受到限制?
setCookie的代码是:
include 'getSounds.php';
$sound = getListAvailableSounds();
krsort($sound);
$audio = "";
$audioDescription = "";
$audioPosition = "";
if(!isset($_COOKIE['audio'])){
setcookie("audio", $sound[count($sound) - 2 ]['name'], time() + 30 * 24 * 60 * 60);
setcookie("audioDescription", $sound[count($sound) - 2 ]['description'], time() + 30 * 24 * 60 * 60);
setcookie("audioPosition", (count($sound) - 1) , time() + 30 * 24 * 60 * 60);
$audio = $sound[count($sound) - 2 ]['name'];
$audioDescription =$sound[count($sound) - 2 ]['description'];
$audioPosition = (count($sound) - 2);
}else{
$audio = isset($_COOKIE['audio']) ? $_COOKIE['audio'] : $audio;
$audioDescription = isset($_COOKIE['audioDescription']) ? $_COOKIE['audioDescription'] : $audioDescription;
$audioPosition = isset($_COOKIE['audioPosition']) ? $_COOKIE['audioPosition'] : $audioPosition;
if($audioPosition > 0){
setcookie("audio", $sound[($audioPosition - 1) ]['name'], time() + 30 * 24 * 60 * 60);
setcookie("audioDescription", $sound[($audioPosition - 1) ]['description'], time() + 30 * 24 * 60 * 60);
setcookie("audioPosition", ($audioPosition - 1) , time() + 30 * 24 * 60 * 60);
} else{
setcookie("audio", $sound[0]['name'], time() + 30 * 24 * 60 * 60);
setcookie("audioDescription", $sound[0]['description'], time() + 30 * 24 * 60 * 60);
setcookie("audioPosition", (count($sound)) , time() + 30 * 24 * 60 * 60);
}
}
header('Content-type: application/json');
echo json_encode(array("audio" => $audio, "audioDescription" => $audioDescription, "audioPosition" => $audioPosition)) ;
对于getSounds:
function getListAvailableSounds(){
$availSounds = array();
$audioPath = __DIR__ . DIRECTORY_SEPARATOR . 'audio' ;
$audioExtArray = array("wav", "aiff", "mp3", "aac", "ogg", "wma");
$flag = true;
$jsonPath = __DIR__ . DIRECTORY_SEPARATOR . 'audio' . DIRECTORY_SEPARATOR . 'files.json';
$getfile = file_get_contents($jsonPath);
$jsonfile = json_decode($getfile);
if ($handle = opendir($audioPath)){
while (false !== ($file = readdir($handle))){
if ($file != "." && $file != ".." && in_array(strtolower(substr($file, strrpos($file, '.') + 1)), $audioExtArray)){
foreach($jsonfile->records as $index => $obj){
if(count($obj) > 0 && $obj[0] == $file){
$pathParts = pathinfo($file);
$tempName = $pathParts['filename'];
if($tempName != "endofaudiofile"){
if($tempName >= strtotime("-48 hours"))
array_push($availSounds, array("name" => $obj[0], "description" => $obj[1]));
}else{
array_push($availSounds, array("name" => $obj[0], "description" => $obj[1]));
}
}
}
}
}
closedir($handle);
}
return $availSounds;
}
答案 0 :(得分:0)
我是个大笨蛋。
音频缺少controls属性。 感谢您的帮助