iframe中的name标记无法滚动



我已经阅读并尝试了许多向下滚动iframe的解决方案,但没有一个对我有效。这是jquery.js完成的定时器回调的javascript函数。当输入文件更改时,我重新生成了页面(src="pp_ContactRangesPanel.php?sc=terra&path=/var/www/html/java_clocks/terra/terra_aos_times&filter=N;#target"),并通过reload()更新了iframe。然后我尝试滚动到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. 在我的测试中,我要么滚动到页面的顶部,要么滚动到底部,然后触摸输入文件开始重新加载

有什么想法吗?为什么它不按目标行编写脚本?

我找到了解决滚动问题的方法。我没有执行obj.contentWindow.location.reload()命令,该命令似乎不包括#target部分,而是将src属性重置为其自身。由于原始src字符串(例如"pp_ContactRangesPanel.php?sc=aqua&path=/var/www/html/java_clocks/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 += '';  

最新更新