【问题标题】:scrolling not working for name tag in iframe滚动不适用于 iframe 中的名称标签
【发布时间】:2016-03-31 05:18:18
【问题描述】:

我已经阅读并尝试了许多向下滚动 iframe 的解决方案,但没有一个对我有用。这是 javascript 函数,是由 jquery.js 完成的计时器回调。当输入文件更改时,我重新生成了页面 (src="pp_ContactRangesPanel.php?sc=terra&path=/var/www/html/java_clocks/terra/terra_aos_times&filter=N;#target") 并通过 reload( )。然后我尝试滚动到 iframe 中的 name="target" 行,但它永远不会移动。输入网页顶部有一个“跳转到目标行”按钮,它可以跳转到目标行,所以我知道重新加载后标签在那里。这是重新加载页面并应滚动到目标行的 javascript 部分。

        // new mtime vs. last mtime test
        if ($aPath[$currentFile][2] != $aPath[$currentFile][3]) {
            $aPath[$currentFile][3] = $aPath[$currentFile][2];

            // read in file again and re-populate mission table
            var obj = document.getElementById($aPath[$currentFile][0]);
            obj.contentWindow.location.reload();

            var doc = (obj.contentDocument)? obj.contentDocument: obj.contentWindow.document;
            var anchors = doc.getElementsByName('target');

            // got target line?
            if (0 < anchors.length)
            {
                doc.scrollTo(0, anchors[0].offsetTop);
            }
        }

有些过头了:

  1. 创建 3 行 iframe 时,iframe 高度设置为 257。
  2. 此 iframe 位于第二行。
  3. 返回的 anchors[0].offsetTop 是 834,这是有道理的,因为生成的 src 网页中有 80 行。
  4. 对于我的测试,我滚动到页面顶部或底部,然后再触摸输入文件以开始重新加载。

那么有什么想法为什么不将脚本写入目标行?

【问题讨论】:

  • 您是否尝试滚动 html 元素而不是 document

标签: javascript iframe scroll


【解决方案1】:

我找到了解决滚动问题的方法。我没有执行似乎不包含#target 部分的 obj.contentWindow.location.reload() 命令,而是将 src 属性重置为其自身。由于原始 src 字符串(例如“pp_ContactRangesPanel.php?sc=aqua&path=/var/www/html/java_clocks/aqua/aqua_aos_times&filter=N;#target”)在字符串末尾包含“#target”,因此 iframe重新生成页面时滚动到它。知道 scrollTo 命令出了什么问题还是很高兴的。

// read in file again and re-populate mission table
var obj = document.getElementById($aPath[$currentFile][0]);
// Note: the obj.contentWindow.location.reload(); way does not remember the #target 
// part so I am resetting the src to the orginal src value thereby forcing it a reload by 
// appending NULL to the end of the string.  I could have gotten the string obj.src and 
// used that, but this seems cleaner.
obj.src += '';  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多