简体   繁体   English

如何更改src并使用DOJO重新加载iframe?

[英]How to change the src and reload an iframe with DOJO?

I want to click on a link, pull the src of the link, and use that src to refresh an iframe in my page. 我想单击一个链接,拉出该链接的src,然后使用该src刷新页面中的iframe。

dojo.ready(function() {
  dojo.query(".link").onclick(function(e) {
    var node    = e.target;
    var url     = dojo.attr(node, "href");
    document.getElementById("audioiframe").src = url;
    dojo.stopEvent(e);
   });
});

I could do this quickly in OG Javascript or jQuery, but need to use DOJO. 我可以在OG Javascript或jQuery中快速完成此操作,但需要使用DOJO。

Thank you 谢谢

UPDATE: After incorporating Strife25 answer my code is: 更新:合并Strife25后,我的代码是:

dojo.ready(function() {
  dojo.query(".audiolink").forEach(function(link){
      dojo.connect(link, "onclick", null, function(e){
        var node    = e.target;
        var url     = dojo.attr(node, "href");
        dojo.query("#audioiframe").attr("src", url);
        dojo.stopEvent(e);
      });
  });
});

The event is attached to the links, the iframe src attribute is updated, but the onclick event is not canceled and the page behaves as if the link is clicked. 事件附加到链接,iframe src属性已更新,但onclick事件未被取消,页面的行为就像单击链接一样。 It appears as if dojo.stopEvent(e); 看起来好像是dojo.stopEvent(e); is not working (asking where to download the MP3 File). 不工作(询问在哪里下载MP3文件)。 Also, the iframe is not reloading but I believe that is due to the click not being canceled. 此外,iframe没有重新加载,但我认为这是由于点击未被取消。

UPDATE: I am the bug, not the code! 更新:我是错误,而不是代码! I was linking to the MP3 versus the page that housed the MP3 (long story). 我当时链接到MP3,而不是MP3所在的页面(长话)。 It works. 有用。 Thank you. 谢谢。

You're attaching the click event in the wrong way, you should use dojo.connect to attach event handlers. 您以错误的方式附加了click事件,应该使用dojo.connect附加事件处理程序。 Also, dojo.query() returns a NodeList when it runs instead of a single node. 此外,dojo.query()在运行时返回NodeList而不是单个节点。

The proper way to use dojo in this instance would be: 在这种情况下使用dojo的正确方法是:

dojo.query(".link").forEach(function(link){
    dojo.connect(link, "onclick", null, function(e){
        //do your stuff
    });
});

If you only have one button that performs the onclick event you are proposing, you should instead use dojo.byId like so: 如果您只有一个按钮来执行您提议的onclick事件,则应该使用dojo.byId,如下所示:

var link = dojo.byId("linkId"); //retrieves a reference to a DOM node with the given id
dojo.connect(link, "onclick", null, function(e){
    //do you stuff
});

Here is the doc page of dojo.connect, which also explains how you can clean up event handlers on object destruction / page unload: http://www.dojotoolkit.org/reference-guide/dojo/connect.html 这是dojo.connect的文档页面,还介绍了如何在对象破坏/页面卸载时清理事件处理程序: http : //www.dojotoolkit.org/reference-guide/dojo/connect.html

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

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