在其他页面上使用Javascript的getElementByI

在其他页面上使用Javascript的getElementByI

本文介绍了如何在其他页面上使用Javascript的getElementById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用同一页面时,我可以使用document.getElementById(),但我一直无法弄清楚如何通过不同页面的ID获取元素。到目前为止,我的猜测是使用jQuery的$ .get函数,我还没有开始工作。

I'm able to use document.getElementById() fine when working with the same page, but I haven't been able to figure out how to get the element by it's ID from a different page. So far, my guess has been to use jQuery's $.get function, which I haven't gotten to work either.

$(function() {
    $('#inputform').submit(function(e) {
        e.preventDefault();
        var rawnum = $('#inputform').serialize();
        var num = rawnum.split("=")[1];
        var url = "http://google.com/"; //this url is an example
        $.get(url, function(data, status) {
            $("html").html(data);
        });
    });
});


推荐答案

您的网页需要成为开门人和其他人页面,更重要的是,这两个页面必须以相同的来源提供服务(请参见)或者其他页面必须通过提供,以确保此访问得到授权。

Your page would need to be the opener and the other page and, more importantly, the two pages would have to be served by the same origin (see same origin policy) or that other page would have to be served with CORS headers precising this access is authorized.

正如您的示例所示,显然不是这种情况,因此您无法访问该元素。

As your example shows, this is obviously not the case so you simply can't access the element.

这篇关于如何在其他页面上使用Javascript的getElementById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:29