Refresh Javascript Src

99封情书 提交于 2019-12-13 09:35:34

问题


How to refresh your result when put you search any keywords. If I use innerHTML it can update the keywords that I find but when use script src i doesnt work, it only work once. This is my coding

  <html>
  <head>
    <title>Google Search API</title>
  </head>
  <body>

    <div id="box">
    <textarea id="myTextarea"></textarea>
   <button type="button" id="mySubmit" onclick="myFunction()" >Search</button>

<p id="demo"></p>
<div id="content">



    </div>


    <script id="searchme"></script>
    <script>
    function myFunction() {
        var query = document.getElementById("myTextarea").value;
        var search_query = "https://www.googleapis.com/customsearch/v1?q="+query+"&cx=004123968310343535430%3Aaxml6iel9yo&key=AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg&callback=hndlr";
    document.getElementById('searchme').src = search_query; 
    }

    function hndlr(response) {
    for (var i = 0; i <response.items.length; i++) {
        var item = response.items[i];     
        document.getElementById("content").innerHTML +="<br>"+"<b>"+ item.title + "</b>"+"<br>" +item.snippet  + "<br>"+"<a href='"+item.link+"'>"+item.displayLink+"</a>" ;
    }
}


</script>


  </body>
</html>

回答1:


Remove the listener from the button and add it the input so it happens as soon as you type.

<textarea onkeydown="myFunction()"></textarea>
<button type="button" id="mySubmit" >Search</button>

Although, I don't recommend the use of onkeydown but this does the job for you.



来源:https://stackoverflow.com/questions/43558232/refresh-javascript-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!