繁体   English   中英

按“ Enter”键启动Google图片搜索

[英]Make google image search initiate on press of 'enter'

http://protected-river-1861.herokuapp.com/链接到我的网站

因此,我需要按“ enter”键启动对Google图片的搜索。 当前,仅在单击按钮时有效。

HTML:

<p>Enter the keyword to search for images</p>
<input id="search-term" type="text">
<button id="go-search">Go!</button>
<div id="search-results"></div>
<div style="width: 150px; margin:0 auto;">

application.js:

$(document).on('click','#go-search',function(){findImagesOnGoogle({keywords:$('#search-term')。val(),container:'#search-results'} )}); $(document).on('click','#search-results img',function(){var url = $(this).data('url'); $(“#workspace img”)。remove() ; var img = $(“”)。attr('src',url); $(“#workspace”)。append(img);});

CSS,JS和HTML都位于Sublime Text中的单独选项卡上。

我认为该《守则》可以为您提供帮助。 我没有为您提供完整的解决方案,而是让您自己学习编写代码。

<input type="text" placeholder="Enter Search Term & Press Enter.." onkeydown="javascript:return checkEnterPressed(event);" />
<script type="text/javascript">
    function checkEnterPressed(thisEvent) {
        if (thisEvent.keyCode == 13) { // 13 : Enter Key
            alert('enter Pressed');
            // Do WHatever you want to do HERE.
            return true; 
        }
    }
</script>

根据您提供的代码,您可以尝试类似

$('#search-term').on("keypress", function(e) {
   if (e.keyCode == 13) {
      findImagesOnGoogle({keywords: $('#search-term').val(), container: '#search-results'})
   }
    });
});

并将其放在最后一行代码之后。

jsFiddle一个例子(不是搜索,而是弹出窗口)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM