Javascript Dojo / Javascript - 如何替换iframe中的链接

时间:2014-11-12 00:56:25

标签: javascript iframe dojo

我目前在我的网站上使用以下dojo脚本来替换包含文本' local-store'的所有链接的部分内容。用文本'培根'。这当前有效,但我现在想把它放在iframe中。我已经将脚本和链接都放在了iframe中,但我仍然只是将本地商店和#39;文字出现在链接中。

<iframe>
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<script type="text/javascript">
require(["dojo/ready", "dojo/query", "dojo/topic", "dojo/domReady!"], function(ready, query, topic, analytics){
ready(function(){
dojo.query('a[href*="local-store"]').forEach(function(link){
var href = link.href;
href = href.replace("local-store", "bacon");
link.setAttribute('href', href); 
  });
});
});
</script>

<div>
<a href="http://domain.com/local-store/category" target="_parent">test link</a>
</div>

</body>
</iframe>

任何想法为什么它不起作用? 感谢

1 个答案:

答案 0 :(得分:0)

上下文可能是根文档,尝试更改它或使用withDoc

require(["dojo/dom", "dojo/_base/window"], function(dom, win) {
    var iframe = dom.byId('iframe').contentWindow;
    win.setContext(iframe.window, iframe.window.document);
    /* your query here */

    /* ------------------ */
    /*   Alternatively    */
    /* ------------------ */

    var iframeDoc = dom.byId("someFrameId").contentWindow.document;
    win.withDoc(iframeDoc, function(){
        /* your query here */
    }, this));
});