简体   繁体   中英

How to access another page's elements to use in javascript? Not JQuery please

Say I have a page a.php and want to get a table element using it's id from another page b.php. Know it have been asked but only answers were in Jquery, any way of doing it with pure js?

Thanks!

If you mean accessing data from another tab/window, that is possible via acquiring a reference to it. For example if you open it yourself:

<html>
    <head>
        <script>
            var otherdoc;
            function create(){
                otherdoc=window.open("about:blank","otherwindow").document;
                otherdoc.open();
                otherdoc.write("<table border='1'><tr><td id='data'>aaa</td><td>ccc</td></tr></table>");
                otherdoc.close();
            }
            function check(){
                alert(otherdoc.getElementById('data').innerHTML);
            }
            function modify(){
                otherdoc.getElementById('data').innerHTML="bbb";
            }
        </script>
    </head>
    <body>
        <button onclick="create()">Create</button>
        <button onclick="check()">Check</button>
        <button onclick="modify()">Modify</button>
    </body>
</html>

When you click on the first button, the window.open line opens a new window/tab, and a reference to its document is kept. "about:blank" could be an actual URL - here instead a table is written there manually.

The other two buttons access and modify a cell in the table.

Suggested sequence for pushing the buttons: Create, Check, Modify, Check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM