php文件可以包含在JavaScript数组中吗

时间:2019-04-30 06:03:37

标签: javascript php jquery arrays

我一直在使用jQuery使某些项目可排序,从而使它们能够四处移动并将其保存到本地存储中:

  $(function () {

$("#sortable").sortable({
    update: function (event, ui) {
        save1();
    }
});
$("#sortable").disableSelection();

var shopping = [];
if (!localStorage.shoppingText) {

    shopping = [ "Meat", "Watermelon", "Apples"];
    shopping.sort();
} else {
    var shoppingText = localStorage.shoppingText;
    shopping = shoppingText.split(",");
}

var textToInsert = "";
$.each(shopping, function (count, item) {
    textToInsert += "<li  class=\"ui-state-default\"><span class=\"ui-icon ui-icon-arrowthick-2-n-s\"></span>" + item + "</li>";
});

$("#sortable").append(textToInsert);

$("#btnSave").click(function (event) {
    // not really required because we will save whenever we update the list
    event.preventDefault();
    save1();
});


function save1() {
    // put items in sortable into an array
    var theArray = [];
    $("li", "#sortable").each(function (count, item) {
        theArray[count] = $(this).text();
    });
    var shoppingText = theArray.toString();
    localStorage.setItem("shoppingText", shoppingText);
    }
 });

我想将每个项目放在自己的.php文件中,并且仍然能够像以前一样对项目进行排序。我尝试通过this question中描述的过程将文件的内容读取到数组中,但是php文件中的代码标记被读入li标记中,或者整个ul标记崩溃,并且没有任何显示。

我正在寻找的输出:

index.php(内容):

//sortable          
<div id="1"><?php include './Meat.php' ?></div>
<div id="2"><?php include './Watermelon.php' ?></div>
<div id="3"><?php include './Apple.php' ?></div>


...shopping = [ Meat.php, Watermelon.php, Apples.php];...

0 个答案:

没有答案