当我单击激活选项卡中的按钮时,如何使其他选项卡单击元素

How to make other tabs click an element when I click a button in the activated tab?

本文关键字:单击 选项 何使 元素 其他 激活 按钮      更新时间:2023-09-26

我有许多打开的页面具有相同的域和相同的内容,我需要的是,当我单击一个选项卡中的特定按钮时,其他选项卡会响应并单击相同的按钮,所以我可以节省很多时间。我在这里的某个地方读到这个功能被称为"对讲机"。

很抱歉延迟回答,但我想写一个好方法,而不仅仅是快速回答分数。仅当页面都位于同一域下时,此操作才有效。LocalStorage就是这样工作的。让我知道这是否适合您,并且可能需要稍作调整,但应该给您一个工作的想法。可能有更好的方法,但这是我一开始会如何处理它的方式。

这将出现在每一页上。然后,您可以使用页面网址或类似隐藏字段的内容,使每个页面都独一无二,并且可以在您当前访问该页面时进行跟踪。但总体思路应该可行。

祝您编码愉快!希望对您有所帮助!

这是有效的,因为该脚本在每个页面上运行。它知道它在哪个页面上的唯一原因,是由于隐藏字段值在执行脚本时表示当前页面 s 的内容。

这是一个非常有道理的例子。但应该给你概念。由于此脚本在每个页面上运行,并且是通用编写的,因此可以正常工作。只需确保每个页面上的"触发事件"按钮名称相同即可!

附言您还可以使用window.location来获取您在instad上隐藏字段的页面。

每个页面的 HTML 示例 *****

<!-- Each Page -->
<!-- Page 1-->
<div>
  <input type="hidden" value="page1"/>
  <input type="button" id="cmdButtonThatTriggersEvent" value="Test Buttom"/>
</div>
<!-- Page 2-->
<div>
  <input type="hidden" value="page2"/>
  <input type="button" id="cmdButtonThatTriggersEvent" value="Test Buttom"/>
</div>
<!-- Page 3-->
<div>
  <input type="hidden" value="page3"/>
  <input type="button" id="yourButton" value="Test Buttom"/>
</div>

//Make sure ever button name or id is the same on each page. 
//That way it is generic. You will also have to track what page //triggered the click event. So make a localstorage hold if the action was clicked to occur, and a localstorage key that holds an array fo all the pages you want this done on. So that way the code can be on every page, and you mark the page off if all page entries were affected, then reset the switch back to null. 
****Javascript example for each page*****

<script> 

       var globalAllPages = ["Page1, "Page3", "Page3"]; //etc.
       $(document).on("click", "#yourButton", function(){
         localStorage.setItem("trigger_state", "true");
         HandleLocalStorageSwitch();
       });
       $(document).ready(function(){           
            setInterval(function(){              
                HandleLocalStorageSwitch();                
            }, 500);
        });

        function HandleLocalStorageSwitch()
        {
             var trigger = localStorage.getItem("trigger_state");
             var location = localStorage.getItem("location");
             if(trigger != undefined && trigger == "true"){
               //now see if this page has ran yet
                var dictPages = JSON.parse(localStorage.getItem("current_pages")); //gets the array  
                var exists = false;
                for(var x=0;x<dictPages;x++){
                   if(dictPages[x]=="this_page"){
                      exists=true;
                      break;
                    }                    
                }
                //if it wasnt found then insert it
                if(!exists){
                   dictPages.add("this_page");
                   $("#this_page_Button").click(); //now trigger this button
                }
                if(dictPages.length == globalAllPages.length){
                    //we hit all pages
                    //then all have been ran, and remove the switch and clear dict
                   localStorage.setItem("shouldTriggerEvent", "false");
                   //so now reset and won't run untill one of buttons clicked, then starts process again
                   localStorage.removeItemKey("current_pages");
                }                
              }                                  
           }
        }