【问题标题】:Search term to url搜索词到 url
【发布时间】:2019-04-22 22:55:09
【问题描述】:

我正在尝试使搜索引擎成为“比较器”。我的意思是我正在尝试制作一个本地网站,该网站采用搜索词并将其变成可点击的搜索引擎 URL(谷歌、必应、雅虎等)。我从谷歌开始。搜索词要跟在https://www.google.com/search?q=后面,不知道怎么把userSearch加到URL后面,把URL变成可点击的链接。我什至不确定我是否正确收集了搜索词。

<!-- Enter search term -->
        <form>
          <fieldset>
            <legend></legend>
            <p>
              <label>Search Here! (please, no spaces)</label>
              <input type = "text"
                     id = "userSearch"
                     var = term
 />
            </p>

          </fieldset>
        </form>



<var> gURL = 'https://www.google.com/search?q=' + userSearch </var>


<script type="text/javascript">
    <var> searchTerm = "userSearch"; </var>
    <var> gURL = "https://www.google.com/search?q=" + 'searchTerm'; </var>

    document.getElementById("gURL").src = url;
</script>


<!-- Search URL results -->


        <h3>Results</h3>
        <fieldset>
        <h4 style="text-align:left;"><a href="https://www.google.com/search?q=">Google.com </a>

如果你能帮忙,谢谢。我很感激。

【问题讨论】:

  • 尝试查看事件监听器。您必须劫持提交按钮才能找到文本输入(可能通过 id),从中获取文本,然后将其附加到您的 url。总的来说,您可能应该在更简单的项目上花费更多时间。点击计数器(例如)会教你很多东西。
  • 感谢您推荐点击计数器。在您回复之前,我确实完成了我想做的事情,但我非常感谢您的反馈。

标签: html var


【解决方案1】:

我使用了一个按钮和 Jquery - 但这是你想要做的吗?

<html>
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</style>
<script type="text/javascript">
$(document).ready(function() {
  $('#userSearch').click(function() {
    var fromForm =  "<br>" + '<a href="https://www.google.com/search?q="' + 
        $('#searchField').val() + ">" + 'https://www.google.com/search?q="' +$('#searchField').val() + '</a>';

    $('#outPut').append(fromForm);

  });

});     
</script>
</head>
<body>


<br>
<!-- Enter search term -->
<form>
          <fieldset>
            <legend></legend>

                 <label>Search Here! (please, no spaces)</label> <input type='text' id='searchField' placeholder="Search">
</fieldset>
</form>
                    <button id='userSearch'>Search</button>

<br>
<div id='outPut'>
    </div>

【讨论】:

    【解决方案2】:

    我能够使用多个指南使用一些不同的代码来制作我的比较器,但我非常感谢您的反馈。我将我的代码放在 Weebly 网站 (Click here) 上,我的目标是让它成为一个工具,可以轻松地从不同的引擎进行搜索。它主要仅供个人使用,但我将它设置在其他人可以发现它有用和有用的地方。

    我使用了这个代码:

        <!-- Enter search term -->
    <div>
    <input name="searchTxt" type="text" maxlength="512" id="searchTxt" class="searchField" float: center; display: block;/>
    </div>
    
    <script type="text/javascript">
            //creates a listener for when you press a key
            window.onkeyup = keyup;
    
            //creates a global Javascript variable
            var inputTextValue;
    
            function keyup(e) {
              //setting your input text to the global Javascript Variable for every key press
              inputTextValue = e.target.value;
    
              //listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
              if (e.keyCode == 13) {
                window.location = "https://www.google.com/search?q=" + inputTextValue;
              }
            }
    </script>
    

    感谢大家的帮助,我一定会继续学习的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-12
      • 2017-05-19
      • 2011-11-23
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多