我想改变Iframe中元素的宽度

时间:2013-12-24 07:37:02

标签: javascript jquery css iframe

我在单个帖子的左侧使用了社交侧边栏。我想以相同的尺寸显示所有按钮,它们现在以不同的尺寸显示。我想让它们像“twitter”按钮一样大小。如需查看,请转到here

我尝试了很多代码但是因为它们的来源是Iframe,这就是为什么我的编码无效。对于谷歌+我使用以下代码:

$('#___plusone_1 iframe').load(function(){
  $('#___plusone_1 iframe').contents().addClass('googleplusbtn');
 }
你能帮我吗?感谢。

我也检查过这些教程,但对我不起作用:

jQuery changing contents of an iFrame

jQuery/JavaScript: accessing contents of an iframe

5 个答案:

答案 0 :(得分:3)

我认为您不能将此作为主页( http://site4preview.site90.net/ )和iframe( http://www.facebook.com/plugins/like.php )的不同之处域。

Jquery内容功能描述:

  

.contents()方法也可用于获取内容文档   iframe,如果iframe与主页位于同一个域中。

来源:JQuery .contents()

答案 1 :(得分:1)

如果iframe网址来自同一个域名。然后你可以使用:

<强> Jquery的:

$("#iFrame").contents().find(".someButtons").addClass("googleplusbtn");

答案 2 :(得分:0)

如果Iframe包含相同应用程序域的页面,则可以通过:

var iframe = document.getElementById('html2');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

获得对象后,现在可以访问:

innerDoc.getElementById('divId').style.width="250px";

答案 3 :(得分:0)

在这里,您可以使用jQuery轻松更改iFrame的宽度。此解决方案与iFrame相同。我创建了一个PHP脚本,可以从其他网站获取所有内容,而最重要的部分是您可以轻松地将自定义jQuery应用于该外部内容。请参考以下脚本,该脚本可以从其他网站获取所有内容,然后您也可以应用自定义jQuery / JS。此内容可以在任何元素或任何页面内的任何位置使用。

<div id='myframe'>

  <?php 
   /* 
    Use below function to display final HTML inside this div
   */

   //Display Frame
   echo displayFrame(); 
  ?>

</div>

<?php

/* 
  Function to display frame from another domain 
*/

function displayFrame()
{
  $webUrl = 'http://[external-web-domain.com]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "
  <script>

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
      */
    //Hide Navigation bar
    jQuery(\".navbar.navbar-default\").hide();

  </script>";

  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;
}

答案 4 :(得分:0)

您可以使用jQuery轻松更改宽度。此解决方案与iFrame相同。我创建了一个PHP脚本,可以从其他网站获取所有内容,而最重要的部分是您可以轻松地将自定义jQuery应用于该外部内容。请参考以下脚本,该脚本可以从其他网站获取所有内容,然后您也可以应用自定义jQuery / JS。此内容可以在任何元素或任何页面内的任何位置使用。

<div id='myframe'>

  <?php 
   /* 
    Use below function to display final HTML inside this div
   */

   //Display Frame
   echo displayFrame(); 
  ?>

</div>

<?php

/* 
  Function to display frame from another domain 
*/

function displayFrame()
{
  $webUrl = 'http://[external-web-domain.com]/';

  //Get HTML from the URL
  $content = file_get_contents($webUrl);

  //Add custom JS to returned HTML content
  $customJS = "
  <script>

      /* Here I am writing a sample jQuery to hide the navigation menu
         You can write your own jQuery for this content
      */
    //Hide Navigation bar
    jQuery(\".navbar.navbar-default\").hide();

  </script>";

  //Append Custom JS with HTML
  $html = $content . $customJS;

  //Return customized HTML
  return $html;
}
相关问题