I want to be able to search 2 websites side-by-side in iframes, using just one search bar. Is my Javascript wrong?

huangapple go评论70阅读模式
英文:

I want to be able to search 2 websites side-by-side in iframes, using just one search bar. Is my Javascript wrong?

问题

我已经在这个页面上使用HTML创建了搜索控制台:

<title>搜索结果</title>
<style>
    iframe {
        width: 100%;
        height: 500px;
        border: none;
        margin-top: 20px;
    }
</style>

<h1>搜索结果</h1>
<form onsubmit="search(event)">
    <input type="text" id="search-box" placeholder="输入关键词">
    <button type="submit">搜索</button>
</form>
<iframe id="search-results"></iframe>
<iframe id="search-results"></iframe>

我的Javascript代码是:

<script>
    function search(event) {
        event.preventDefault();
        var keywords = document.getElementById('search-box').value;
        var url1 = 'https://www.vistaprint.fr/search?query=' + keywords;
        var url2 = 'https://www.vistaprint.com/search?query=' + keywords;
        var iframe = document.getElementById('search-results');
        iframe.src = url1 + '&' + url2;
    }
</script>

理想情况下,我希望看到两个并排显示的iframe作为搜索结果,但在这种情况下只显示了"url1"。我是否漏掉了一部分代码?

英文:

I have created the search console in HTML on a page here:

 &lt;title&gt;Search Results&lt;/title&gt;
	&lt;style&gt;
		iframe {
			width: 100%;
			height: 500px;
			border: none;
			margin-top: 20px;
		}
	&lt;/style&gt;


	&lt;h1&gt;Search Results&lt;/h1&gt;
	&lt;form onsubmit=&quot;search(event)&quot;&gt;
		&lt;input type=&quot;text&quot; id=&quot;search-box&quot; placeholder=&quot;Enter keywords&quot;&gt;
		&lt;button type=&quot;submit&quot;&gt;Search&lt;/button&gt;
	&lt;/form&gt;
	&lt;iframe id=&quot;search-results&quot;&gt;&lt;/iframe&gt;
    &lt;iframe id=&quot;search-results&quot;&gt;&lt;/iframe&gt;

My Javascript is:

 &lt;script&gt;
		function search(event) {
			event.preventDefault();
			var keywords = document.getElementById(&#39;search-box&#39;).value;
			var url1 = &#39;https://www.vistaprint.fr/search?query=&#39; + keywords;
			var url2 = &#39;https://www.vistaprint.com/search?query=&#39; + keywords;
			var iframe = document.getElementById(&#39;search-results&#39;);
			iframe.src = url1 + &#39;&amp;&#39; + url2;
			}
	&lt;/script&gt;

Ideally, I would like to see two iframes shown side-by-side as search results, but only 'url1' shows in this instance. Am I missing a piece of code?

Ideally, I would like to see two iframes shown side-by-side as search results, but only 'url1' shows in this instance. Am I missing a piece of code?

答案1

得分: 0

你代码中的问题就像你的问题评论中已经解释的那样,你正在使用 'getElementById',它会返回一个元素。

你可以为你的iframe元素添加标记,这样你可以使用 'getElementsByTagName',它将返回一个包含2个元素的数组。

之后,你可以像这样为你的iframe元素添加src:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Search Results</title>
  </head>
  <body>
    <h1>Search Results</h1>
    <form onsubmit="search(event)">
      <input type="text" id="search-box" placeholder="Enter keywords" />
      <button type="submit">Search</button>
    </form>
    <iframe id="search-results-fr" name="iframe"></iframe>
    <iframe id="search-results-en" name="iframe"></iframe>
  </body>
  <script>
    function search(event) {
      event.preventDefault();
      var keywords = document.getElementById("search-box").value;
      var url1 = "https://www.vistaprint.fr/search?query=" + keywords;
      var url2 = "https://www.vistaprint.com/search?query=" + keywords;
      var iframe = document.getElementsByTagName("iframe");
      iframe[0].src = url1;
      iframe[1].src = url2;
    }
  </script>
</html>
英文:

The problem in your code is like the comment to your question already explained, that you are using 'getElementById' wich gives you 1 element back.

You could add tags to your iframe elements so you can use 'getElementsByTagName' which will result in an array of 2 elements.

After this you can add the src as you like to your iframe elements like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;Search Results&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Search Results&lt;/h1&gt;
    &lt;form onsubmit=&quot;search(event)&quot;&gt;
      &lt;input type=&quot;text&quot; id=&quot;search-box&quot; placeholder=&quot;Enter keywords&quot; /&gt;
      &lt;button type=&quot;submit&quot;&gt;Search&lt;/button&gt;
    &lt;/form&gt;
    &lt;iframe id=&quot;search-results-fr&quot; name=&quot;iframe&quot;&gt;&lt;/iframe&gt;
    &lt;iframe id=&quot;search-results-en&quot; name=&quot;iframe&quot;&gt;&lt;/iframe&gt;
  &lt;/body&gt;
  &lt;script&gt;
    function search(event) {
      event.preventDefault();
      var keywords = document.getElementById(&quot;search-box&quot;).value;
      var url1 = &quot;https://www.vistaprint.fr/search?query=&quot; + keywords;
      var url2 = &quot;https://www.vistaprint.com/search?query=&quot; + keywords;
      var iframe = document.getElementsByTagName(&quot;iframe&quot;);
      iframe[0].src = url1;
      iframe[1].src = url2;
    }
  &lt;/script&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 07:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定