通过Javascript无效地向Weebly添加元素

时间:2017-03-05 23:21:54

标签: javascript weebly

我有一些Javascript代码可以读取已发布的Google表格并将其读取到网页上。出于某种原因,在嵌入这个int Weebly时,它只是不想接受它。据我所知,根本没有创建任何元素。这是我的代码,任何帮助都很棒。

<!DOCTYPE html><!doctype html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
var spData = null;
function doData(json) {
    spData = json.feed.entry;
}
function readData(parent) {
    var data = spData;
    var ScoreObjs = [];
    var ScoreObj = new Object();
    for(var r=4; r < data.length; r++) {
        var cell = data[r]["gs$cell"];
        var val = cell["$t"];
        console.log(val);
        if (cell.col == 1) {
            ScoreObj.team1 = val;
        }
        else if (cell.col == 2) {
            ScoreObj.team2 = val;
        }
        else if (cell.col == 3) {
            ScoreObj.score1 = val;
        }
        else if (cell.col == 4) {
            ScoreObj.score2 = val;

            ScoreObjs.push(ScoreObj);
            ScoreObj = new Object();
        }
    }
    toFormat(ScoreObjs);
}
function toFormat(obj) {
    for (var x = 0; x < obj.length; x++)
    {
        var data = obj[x];
        var team1 = data.team1;
        var team2 = data.team2;
        var score1 = data.score1.toString();
        var score2 = data.score2.toString();

        var child1 = document.createElement("div");
        child1.className = "paragraph";
        document.body.appendChild(child1);
        var child2 = document.createElement("strong");
        child1.appendChild(child2);
        var child3 = document.createElement("font");
        child3.color = "#FFFFFF";
        child3.innerHTML = team1 + " vs " + team2 + "<br />ESEA Scrim";
        child2.appendChild(child3);

        var child4 = document.createElement("font");
        child4.color = "#00FF00";
        child4.innerHTML = score1 + ":" + score2;
        child1.appendChild(child4);
    }
}
$(document).ready(function(){
    readData($("#data"));
});

</script>
<script src="https://spreadsheets.google.com/feeds/cells/1BSsomIQwFhifrFB8ybQ6xF0t-KdJNKtCBHotY3X8O98/1/public/values?alt=json-in-script&callback=doData"></script>
</head>
</html>

2 个答案:

答案 0 :(得分:1)

变化:

<script
src="https://spreadsheets.google.com/feeds/cells/1BSsomIQwFhifrFB8ybQ6xF0t-KdJNKtCBHotY3X8O98/1/public/values?alt=json-in-script&callback=doData"></script>

要:

<script id="data"
src="https://spreadsheets.google.com/feeds/cells/1BSsomIQwFhifrFB8ybQ6xF0t-KdJNKtCBHotY3X8O98/1/public/values?alt=json-in-script&callback=doData"></script>

答案 1 :(得分:0)

这可能是因为javascript代码首先运行并呈现一些问题。我遇到了同样的问题。

在页脚中包含脚本标记,而不是在head部分中。 实际上,您会在页脚下方找到默认的Weebly脚本标记。添加您的自定义JavaScript代码。

OR (更优惠) 创建一个javascript(.js)文件,将其外部上传到weebly主题编辑器中的Assets,然后将其与其他默认的weebly脚本标记一起添加到HTML头文件中。

例如。将添加一个名为editsite.js的javascript文件(上传到资源后):

# in MyUser model
def get_avatar(self):
   if not self.Profile_image:
       return "YOUR DEFAULT IMAGE"
   else:
       return return os.path.join(settings.MEDIA_URL, self.Profile_image.name)

# and in template:
<img src={each_model.get_avatar} />