javascript 从另一个页面通过 Id 获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15408839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get element by Id from another page
提问by marius
I need to change content of page1 if page2 contain specified element. This code works great if I get id from the same page
如果 page2 包含指定元素,我需要更改 page1 的内容。如果我从同一页面获取 id,则此代码效果很好
if (document.getElementById("page_element")) {
var str = document.getElementById("destination").innerHTML;
var n = str.replace("Login", "Logout");
document.getElementById("destination").innerHTML = n;
}
But I need to check if element is present on page2 in order to change content of page1.
但是我需要检查 page2 上是否存在元素以更改 page1 的内容。
I must change this
我必须改变这个
if (document.getElementById("page1_element"))
with correct path which is page2 (logged.html in my case). How can I do that ?
正确的路径是 page2 (在我的例子中是logged.html)。我怎样才能做到这一点 ?
回答by andlrc
Think this will work...
认为这会奏效...
var iframe = document.getElementById("iframeId"),
doc = getFrameDocument(iframe);
doc.getElementById("page2_element_id"); // [DOM Element]
function getFrameDocument(iframe){
return iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
}
return cond ? a : b
means:
return cond ? a : b
方法:
if ( cond ) {
return a;
}
else {
return b;
}
回答by David Gilbertson
Think of it this way. JavaScript runs in the browser, say, Chrome. Chrome can see the page that is currently loaded, but has no knowledge of the html files that are still on the server, until they are brought into the browser. What you could do is have an iFrame containing logged.html that isn't visible (maybe 1x1 pixels, or a fixed position with a position off the page (left: 4000px). Then you can refer to the iFrame and get the element from there, this one explains how to reference an element in an iFrame: getElementById from iframe
这么想吧。JavaScript 在浏览器中运行,比如 Chrome。Chrome 可以看到当前加载的页面,但不知道服务器上的 html 文件,直到它们被带入浏览器。你可以做的是有一个包含不可见的logged.html的iFrame(可能是1x1像素,或者是一个固定的位置和页面外的位置(左:4000px)。然后你可以参考iFrame并从中获取元素在那里,这个解释了如何引用 iFrame 中的元素:getElementById from iframe
回答by chippie3000
I think this might help you... Don't be too hard on me. This is my first post :p
我想这可能对你有帮助...不要对我太苛刻。这是我的第一篇文章:p
var element_change_content_page1 = "element_id_page1";
var page2_datatype = "html";
var filename_page2 = "logged.html";
var target_element_id_page2 = "element_id_page2";
var target_element_type_page2 = "div"
$.get( filename_page2 , function(response, status){
//You can also use jQuery " $.post() " Method instead of " $.get() "
//Source: http://www.w3schools.com/jquery/ajax_get.asp
var data = $('<div>' + response + '</div>');
var target_element = data.find( target_element_type_page2 + '#'+ target_element_id_page2 )[0]; //There "might" be more div elements with the same id: specify index
if(typeof target_element !== 'undefined'){
//Page2 contains specified element
var container = document.getElementById( element_change_content_page1 );
container.innerHTML = "Content is changed!!!";
} else {
//Page2 DOES NOT contain specified element
}
}, page2_datatype);