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

时间:2023-01-06 00:41:40

I have code like this (http://jsfiddle.net/tb7E9/1/):

我有这样的代码(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>

The second 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>

I'd like to display only style (style1(normal text) or style2(bold text) as example in jsfiddle). It means if I choose style1 for display, then style2 is disable.

我只想显示样式(style1(普通文本)或style2(粗体文本),如jsfiddle中的示例)。这意味着如果我选择style1作为显示,则style2是禁用的。

How can I change code in the second script to do that?

如何修改第二个脚本中的代码来实现这一点?

1 个解决方案

#1


1  

This is the correct way to archive it.

这是正确的存档方式。

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

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

}
Y():

console.print(window.X):

#1


1  

This is the correct way to archive it.

这是正确的存档方式。

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

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

}
Y():

console.print(window.X):