为什么我的onClick js函数调用每页加载只能工作一次?

时间:2013-09-21 19:23:45

标签: javascript html5 rss yql setattribute

我正在尝试使用yql从谷歌新闻中显示RSS订阅源。目前,如果您在表单中输入邮政编码,它可以运行一次,但如果不刷新页面,我就无法输入其他代码。任何帮助将不胜感激!
                               P1

        <script>   
        function top_stories(o){  
            var items = o.query.results.item;  
            var output = '';  
            var no_items=items.length; 

            for(var i = 0; i < 4; i++) {  
            var title = items[i].title;  
            var link = items[i].link;  
            var desc = items[i].description;  
           output += "<h3><a href='" + link + "'>"+title+"</a></h3>" + desc + "<hr/>";  
            }
         document.getElementById('results').innerHTML = output;    
         }  
         </script> 
    </head>
    <body>
        <form name="input" action="" method="get" id="zc">
        Zip Code: <input id="zip" type="text" name="zip">
    <input id ="zipbutton" type="Button" value="Submit"    onClick="showDiv(document.getElementById('zc').zip.value);" >

    <div id="results"  ></div> 
    <script id="yql"></script>
    <script>    
    function showDiv(zip) {             
    var a = "http://query.yahooapis.com/v1/public /yql?q=select%20*%20from%20rss%20where%20url%20%3D%20'http%3A%2F%2Fnews.google.com%2Fnews%3Fgeo%3D" + zip + "%26output%3Drss'%0A&format=json&diagnostics=true&callback=top_stories";
     document.getElementById("yql").setAttribute("src",a);                              
        }
    </script>
    </body>

1 个答案:

答案 0 :(得分:0)

尝试为每次点击创建新的<script>元素。

var script = document.createElement('script');
script.setAttribute("src", a);
document.getElementsByTagName('head')[0].appendChild(script);