-1

我目前正在建立一个网站,作为我 11 年媒体课程的一部分,但我似乎无法让这个 If...Else... 功能正常工作。这是我的编码。

if(ifr.contentDocument.getElementById("ctl00_ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_MessagesTextWrapper") != null){
    notify("Personalising site...")
    document.getElementById("loginButton").innerHTML = "Account Connected!";
    document.getElementById("loginButton").onclick=function(){window.location.href="//roblox.com/--place?id=91276386";}
    notify("Account connected successfully");
    hideWheel();
}else{
    notify("Could not connect account");
    hideWheel();
};

我删除了“if(...){...}else{...};” 从代码中发现它有效,所以我确定故障出在“if(...)”本身。

如果有帮助,ifr 实际上是使用 document.createElement("iframe"); 创建的。iFrame 确实有一个“src”,并且确实显示在显示内容的页面上。

任何帮助表示赞赏。谢谢。

4

2 回答 2

1

尝试使用:

ifr.contentWindow.document

也可以参考:如何使用 Javascript 访问 iframe 元素?

于 2012-09-28T22:01:18.463 回答
0

试试这个:

var x = document.getElementById("ctl00_ctl00_cphBanner_MenuRedesign_BannerAlertsAndOptionsLoginView_BannerAlertsAndOptions_Authenticated_MessagesTextWrapper");
var y = (x.contentWindow || x.contentDocument);

if(y.document){

    notify("Personalising site...")
    document.getElementById("loginButton").innerHTML = "Account Connected!";
    document.getElementById("loginButton").onclick=function(){window.location.href="//roblox.com/--place?id=91276386";}
    notify("Account connected successfully");
    hideWheel();

}
else{

    notify("Could not connect account");
    hideWheel();

};

您尝试访问的 iframe 和您尝试访问的页面是否在同一个域上?如果它在另一个域上,由于跨域封锁,您无法访问它!

我也对那个长ID感到惊讶,那个ID正确吗?

于 2012-09-28T22:00:03.110 回答