javascript newwindow.document.getElementById 有时不起作用

标签 javascript settimeout getelementbyid window.open

我正在尝试访问新打开的窗口(window.open)上的元素,然后更改其 style.display 属性以使其可见。我尝试过不同的版本,但在我最好的版本中,它只能在某些时候起作用。它似乎受到正在处理的其他事物数量的影响,因为当我打开第三个窗口时,它需要很长时间并且不起作用。有时它甚至不适用于第一个打开的窗口。

    var poppy=null;
    function openAlf(anch,id) {
    poppy=window.open("http://mrsbos.wikispaces.com/Alphabetical+Code+list#"+id,
    "_blank"," width=550, height=600, left=500,top=0");

    setTimeout(showEl,1000);

     }

    function showEl(){

    poppy.document.getElementById('area000t').style.display="block";
    }

在另一个版本中,我将area_id传递给showEl函数。该函数也不起作用,这很遗憾,因为我需要能够使用openAlf()函数打开不同的元素。我认为这是一个时间问题。有谁知道我能做些什么吗?

或者有没有一种方法可以让我从按字母顺序排列的页面中读取元素,而无需打开它,然后使用 window.open 和 mywindow.write 将它们写入新窗口

最佳答案

      var poppy=null;




      function openAlf(anch,id) {
        poppy=window.open("http://mrsbos.wikispaces.com/Alphabetical+Code+list",
       "_blank"," width=550, height=600, scrollbars=yes,left=500,top=0");

      readyState=setTimeout(function(){checker(id) },1000);
       }

      function checker(id){
      console.log("checker");
      try{
      console.log("try");

      poppy.document.getElementById(id).style.display="block";
      clearInterval(readyState);
       }

       catch(err){
       console.log("catch");
       readyState=setTimeout(function(){checker(id) },1000);
       }
       }

这个解决方案有效

关于javascript newwindow.document.getElementById 有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832730/

相关文章:

javascript - 使用jquery的第n个元素

javascript - 允许在 Firestore 中更新特定的嵌套值

javascript - jquery bubblepopup 在文本框上单击时出现

javascript - setInterval 函数 JavaScript 的问题

javascript - Ajax:如何获取具有动态生成的 ID 的元素的值

javascript - Angular 2+一次性绑定(bind)

Javascript setTimeout 立即在 React Native 中运行

javascript - 清除超时并卸载组件后,超时继续触发并执行

javascript - 为什么我的函数不执行?

javascript - 具有 ID 的 DOM 树元素是否成为全局属性?