如何在函数中创建全局变量?

时间:2013-10-16 17:45:28

标签: javascript html json blogger

我有这样的代码(http://jsfiddle.net/tb7E9/1/):

<script>
    function listEvents(json) {
        var feed = json.feed;
        for (var i = 0; i < 3; i++) {
            var entry = json.feed.entry[i];
            var posttitle = entry.title.$t;
            //Style
            if (i == 0) {
                x = "<span style='color:red'>Style 1 </span><p>" + posttitle + "</p>";
                p = "<span style='color:red'>Style 2 </span><h4>" + posttitle + "</h4><p>";
            }
            if (i == 1) {
                y = "<p>" + posttitle + "</p>";
                g = "<h4>" + posttitle + "</h4></p>";
            }
        }
        style1 = x + y; document.write(style1);
        style2 = p + g; document.write(style2);
    }
</script>

第二个脚本:

<script>
    //How to style1 is actived and style2 is disable here?
    document.write("<script src=\"http://www.allbloggertricks.com/feeds/posts/default/-/Tutorials?orderby=published&alt=json-in-script&callback=listEvents\"><\/script>");
</script>

我想在jsfiddle中只显示样式(style1(普通文本)或style2(粗体文本))。这意味着如果我选择style1进行显示,那么style2将被禁用。

如何更改第二个脚本中的代码呢?

1 个答案:

答案 0 :(得分:1)

这是归档它的正确方法。

function Y (){
  var window.X = *** ;

  console.print(window.X):
  console.print(X):

}
Y():

console.print(window.X):