javascript - 扩展表格和 iframe 的脚本

标签 javascript iframe width expansion

我有一个脚本可以扩展表格中的 iframe 宽度。该代码之前曾运行过,并且可以在 Internet Explorer 中运行。 javascript函数是调用展开箭头的onclick。

这是表格代码:

<table cellspacing="0" cellpadding="0" style="border: 1px solid #000000;">
    <tbody>
        <tr>
            <td style="background-color: rgb(232, 226, 210);border-bottom-style: solid;border-right-style:solid;border-width:1px;border-bottom-style:solid;border-width:1px;">
                <p class="headings">
                    iWannaGo.co Best Deals 
                    <a onClick="setContract()" title="Minimise Fares Window">
                    <img alt="<" width="20" height="20" src="/wp-content/themes/thesis_18/custom/images/left.png"></a>
                    Expand
                    <a onClick="setExpand()" title="Maximise Fares Window">
                        <img alt=">"  width="20" height="20" src="/wp-content/themes/thesis_18/custom/images/right.png" >
                    </a>
                </p>
            </td>
            <td style="background-color: rgb(232, 226, 210);border-bottom-style:solid;border-width:1px;">
                <p class="headings">Compare Recommended Sites</p>
            </td>
        </tr>
        <tr>
            <td id="fare" style="border-right-style:solid;border-width:1px;vertical-align:top;">
                <iframe src="<?php echo $search_fb; ?>" id="farebuzz" scrolling="auto" width=800 height=400 frameborder="0" align="left" style="overflow-x: hidden;padding-left:10px;padding-right:10px;"></iframe>
              </td>
            <td id="bwix">
                <iframe src="<?php echo $search_bw; ?>" id="bwiz" scrolling="auto" width=300 height=300 frameborder="0"  style=""> </iframe>
            </td>
        </tr>
    </tbody>
</table>

然后是 JavaScript 函数。

 function setExpand() {
      var iframeElement = parent.document.getElementById('farebuzz');
      var iframeElements = parent.document.getElementById('bwiz');
      var tableElement = document.getElementById('fare');  
      var tableElements = document.getElementById('bwix'); 
      iframeElement.style.height = 400; 
      iframeElement.style.width = 960;
      iframeElements.style.width=150;
      tableElement.style.width=960;
      tableElements.style.width=150; 
 }
 function setContract() {
      var iframeElement = parent.document.getElementById('farebuzz');
      var iframeElements = parent.document.getElementById('bwiz');
      var tableElement = document.getElementById('fare');  
      var tableElements = document.getElementById('bwix'); 
      iframeElement.style.height = 400;
      //100px or 100% 
      iframeElement.style.width = 800;
      //100px or 100% 
      tableElement.style.width=800;
      tableElements.style.width=300;
      iframeElements.style.width=300; 
 }
 function expanddiv() { 
      var padElements = document.getElementById('pad');  
      padElements.style.display='block'; 
 }

这是代码的工作形式和非工作形式:

链接工作扩展在 IE 中工作,这就是 真的很困惑。

最佳答案

您不能将 style.width 设置为数字,它必须是字符串,并且必须包含 px 后缀:

iframeElement.style.height = "400px";

编辑:此外,请考虑对图像的 alt 属性中的尖括号进行编码。应该是:

<img alt="&lt;" ... />
<img alt="&gt;" ... />

这可能不会立即造成问题,但在可预见的将来肯定会造成问题。它已经搞乱了 SO 的代码格式。 :-)

关于javascript - 扩展表格和 iframe 的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5652366/

相关文章:

javascript - 为什么多功能代码行不会产生错误而只是停止执行?

javascript - 在 JavaScript 中生成特定范围内的随机整数

javascript - 从 iframe 访问父页面的 id 元素

javascript - 如何从内部调用 iframe。

javascript - 更改 iframe 中 body 元素的背景颜色

html - float div 宽度的 MPDF 问题

javascript - jquery鼠标悬停颜色变化

javascript - Electron 模态javascript按钮不起作用

css - 100% 窗口高度 | 100% 父 div 宽度

ios - Cordova应用程序宽度与IOS Safari宽度不同