如何在 Google 搜索 iframe 上设置自定义宽度?

发布于 2024-07-09 05:08:56 字数 781 浏览 6 评论 0 原文

我正在尝试对我的网站使用谷歌搜索:

http://www.houseofhawkins.com/search.php< /a>

在某些屏幕分辨率下效果不佳。 以下是 google 给出的代码:

<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 250;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>

我将“googleSearchFrameWidth”更改为 250,认为应该以 px 为单位设置宽度(一开始是 600)。 但对于较小的屏幕(1024 * 768),它会伸出我的 div 的一侧。

我是不是在做蠢事?

I am trying to use google search for my site:

http://www.houseofhawkins.com/search.php

It is not playing nice with some screen resolutions. Here is the code given from google:

<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 250;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>

I changed the "googleSearchFrameWidth" down to 250 thinking that should be setting the width in px, (it was 600 to start with). But with smaller screens (1024 * 768) it sticks out the side of my divs.

Am I doing something stupid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

想念有你 2024-07-16 05:08:56

我有三个设置可供您调整,我希望这三个设置的组合能够帮助您到达您需要的位置:

  1. googleSearchFrameWidth - 在 JavaScript 中将其设置为您想要的宽度。 这是最明显的一个,您可能已经调整过了。
  2. cse-search-results div 的宽度 - 使用内联样式语句(例如 style ="width:500")设置 div 的宽度。
  3. 设置 IFrame 样式 - 如果您检查,您可能会注意到所有这些内容都是通过 IFrame 从 Google 呈现的。 这有点难以调整,但也是可行的。 为此,请在样式表末尾添加一条语句,例如 #cse-search-results iframe { },在括号内包含实际的 iframe 样式。

我希望其中之一或这些的组合能够成为您的答案。 如有疑问,请使用 DOM 检查器(例如 Firebug 中提供的检查器)来分析更改对 DOM 的影响。 希望这可以帮助。

I have three settings that you can tweak, a combination of which I hope will get you where you need to go:

  1. googleSearchFrameWidth - Set this in the JavaScript to your desired width. This is the most obvious one and the one you've probably already tweaked.
  2. Width of cse-search-results div - Use an inline style statement (e.g. style ="width:500") to set the width of the div.
  3. Set the IFrame Style - If you check, you'll probably notice that all of this content is being rendered from Google via an IFrame. This is a bit harder to tweak but doable. To do this, add a statement to the end of your stylesheet such as #cse-search-results iframe { }, including the actual iframe style within the brackets.

I hope one of these or a combination of these proves to be the answer for you. When in doubt, use a DOM inspector such as the one available in Firebug to analyze the impact of your changes on the DOM. Hope this helps.

云朵有点甜 2024-07-16 05:08:56

更新 22/06/10 - 找到了一个更简单的解决方案,如下所示。 将代码中的 FORID 值更改为 FORID:11。 因此,您的一行代码应类似于以下内容:

<input type="hidden" name="cof" value="FORID:11;NB:1" />

尝试仅添加

#cse-search-results iframe {width: 100%;}

CSS。

UPDATE 22/06/10 - Found a simpler solution that below. Change the FORID value in the in code to FORID:11. Therefore one line of you code should look similar to this:

<input type="hidden" name="cof" value="FORID:11;NB:1" />

Try just adding

#cse-search-results iframe {width: 100%;}

in your CSS.

小帐篷 2024-07-16 05:08:56

遇到了与您想要调整 iFrame 大小相同的麻烦。 您可能认为更改 googleSearchFrameWidth 值就可以解决问题,但事实并非如此。

所以我求助于 DOM 操作。 由于 iFrame 的名称是“googleSearchFrame”,因此在引用之后

<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>

,我添加了另一个

<script type="text/javascript">
  document.getElementsByName('googleSearchFrame').item(0).width = 600;
</script>

上面设置了 iFrame 的宽度位于600px。 显然,就您而言,您希望将其设置为250。 如果您担心 Google 有一天可能会更改 iFrame 名称,只需使用 getElementsByTagName('iFrame') 方法并将其范围缩小到 iFrame 在文档中的位置(如果您有多个 iFrame)。

Ran into the same trouble you did wanting to resize the iFrame. You'd think changing the googleSearchFrameWidth value would do the trick, but nope.

So I resorted to DOM manipulation. Since the name of the iFrame is "googleSearchFrame", Right after the

<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>

reference, I added another <script> tag with the following:

<script type="text/javascript">
  document.getElementsByName('googleSearchFrame').item(0).width = 600;
</script>

The above sets the width of the iFrame at 600px. In your case, obviously, you'd want to set it to 250. If you're paranoid that Google might change the iFrame name some day, just go with the getElementsByTagName('iFrame') method and narrow it down to the position of your iFrame in the document (if you have multiple iFrames).

享受孤独 2024-07-16 05:08:56

修改网站中 googleSearchFrameWidth 的简单步骤:

  1. http://www.google.com/afsonline/show_afs_search.js 并将其保存在 results.htm 网页所在的同一目录中。
  2. 打开您的 results.htm 网页并将行代码修改到脚本中
    ... src="http://www.google.com/afsonline/show_afs_search.js" TO: src="show_afs_search.js"

  3. 打开您自己的“show_afs_search.js”文件并修改设置 width:a
    宽度:“600”
    (或任何您想要设置宽度(以像素为单位)的内容)并保存您自己的“show_afs_search.js”文件。

  4. 将这两个文件上传到托管站点的同一目录:results.htm 和 show_afs_search.js'

完成!

Easy steps to modify the googleSearchFrameWidth in your site:

  1. DOWNLOAD your own 'show_afs_search.js' file from http://www.google.com/afsonline/show_afs_search.js and save it in the same directory where you have you results.htm webpage.
  2. Open your results.htm webpage and MODIFY the line code into the script
    ... src="http://www.google.com/afsonline/show_afs_search.js" TO: src="show_afs_search.js"

  3. Open your own 'show_afs_search.js' file and MODIFY the setting width:a
    to width:"600"
    (or whatever you want to set the width in pixels) and save your own 'show_afs_search.js' file.

  4. Upload both files to your hosting site at the same directory: results.htm and show_afs_search.js'

DONE!

夏有森光若流苏 2024-07-16 05:08:56
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("iframe[name='googleSearchFrame']").css("width","100%");
	});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("iframe[name='googleSearchFrame']").css("width","100%");
	});
</script>

傲娇萝莉攻 2024-07-16 05:08:56

Google 不允许您使用小于 500px 的宽度。 最好的选择是为 iframe 创建一种样式:

<style>
#cse-search-results iframe {width: 200px; }
</style>

Google will not allow you to use a width smaller than 500px. Your best bet is to create a style for the iframe:

<style>
#cse-search-results iframe {width: 200px; }
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文