简体   繁体   English

iframe中对象的.click函数

[英].click function for an object inside an iframe

I'm starting in JS and JQuery, and I just need a bit of help. 我从JS和JQuery开始,我只需要一点帮助。 I want to scroll a page (this is ok), but when a button is clicked. 我想滚动一个页面(可以),但是单击按钮时。 This button has the class "testelement" and is inside an iframe. 此按钮的类别为“ testelement”,位于iframe中。 So I want the main page to be scrolled when a button inside the iframe is clicked. 因此,我希望单击iframe中的按钮时可以滚动主页。 I tried the following code, but it doesn't work. 我尝试了以下代码,但是它不起作用。 I'm sure it's a stupid mistake at the first line, but I don't know... 我确定这是第一行的愚蠢错误,但我不知道...

$('iframe').contents().find('.testelement').click(function() {
    $('body').animate({ scrollTop: 1000 }, 2000);
});

Thanks 谢谢

如果点击确实有效,则应考虑以这种方式进行动画处理

$("html, body").animate({ scrollTop: "1000px" }, 2000);

What you need to do is reference the parent window. 您需要参考父窗口。 See this link window.parent , for more information. 有关更多信息,请参见此链接window.parent Using jQuery, you can try the following. 使用jQuery,您可以尝试以下方法。 Hope this helps: 希望这可以帮助:

$('body', window.parent.document).animate({ scrollTop: 1000 }, 2000);; $('body',window.parent.document).animate({scrollTop:1000},2000);;

EDIT : 编辑:

From the parent window, you can access the button in the iframe (assuming same origin), like this---pretty much just what you wrote above: 在父窗口中,您可以访问iframe中的按钮(假设来源相同),就像这样-就像您在上面写的一样:

$("#MyIframe").contents().find("#scroller").click(function(){
    $('body').animate({ scrollTop: 1000 }, 2000);
}

'#scroller' is the ID of the button on the iframe (or whatever unique selector you want to use). “ #scroller”是iframe(或您要使用的任何唯一选择器)上按钮的ID。

Thanks to everybody, I finally solved my problem ! 谢谢大家,我终于解决了我的问题!

I asked this question because I tried, and it was impossible to click any button in the iframe. 我问这个问题是因为我尝试过,因此无法单击iframe中的任何按钮。 I tried so many things... I became crazy when I discovered that I just had to put it on a server instead of running it locally. 我尝试了很多事情...当我发现我只需要将其放在服务器上而不是在本地运行时,我就变得疯狂。 I felt so stupid ! 我真傻!

FOR EVERY USER OF ADOBE WEB GENERATING SOFTWARES (like ADOBE MUSE, ADOBE EDGE) : 对于ADOBE Web生成软件的每个用户(例如ADOBE MUSE,ADOBE EDGE):

If we run a preview of our work, the software creates a "fake" server, and it works great. 如果我们预览工作,该软件会创建一个“假”服务器,并且效果很好。 But when we generate the final html code, problems happens... It's really often a problem that happens because we're running our page locally. 但是,当我们生成最终的html代码时,就会发生问题...实际上,由于我们是在本地运行页面,因此经常会发生问题。

I hope I helped people 我希望我能帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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