强制IFrame中的任何HREF使用其父级作为目标 [英] Force any HREF in an IFrame to use its parent as the target
问题描述
我目前正在使用IFrame在网站上对用户生成的内容进行沙盒处理。这消除了我们的主样式表的任何样式问题。
I'm currently using an IFrame to sandbox user generated content on a website. This eliminates any styling issues with our main stylesheets.
但是,当用户使用我们的富文本编辑器生成链接时,我们希望链接在父样式中打开不只是打开IFrame中的链接。我意识到你可以为父母设定目标,但是我们无法控制用户以及他们在内容中输入的内容。
However, when a user generates a link using our rich text editor, we would like the link to open in the parent and not just open the link in the IFrame. I realize you can set a target to the parent, but we do not have control of the user and what they enter in their content.
有没有办法劫持IFrame中的HREF所以它们都是目标父对象而不修改它们?或者使用一些可以普遍注入的Javascript,这样我就不需要抓取所有内容并以编程方式替换目标?
Is there any way to hijack the HREFs inside the IFrame so they all target parent without modifying them? Or use a bit of Javascript that could be injected universally so I do not need to scrape through all of the content and replace the target programatically?
理想情况下,一个位置的简单脚本将是最佳解决方案。
Ideally a simple script in one spot would be the best solution.
思考?
结束解决方案
我使用了我选择的答案的变体...它让我进来正确的方向。
I used a variation of the answer I selected... It got me in the right direction.
<script>
Event.observe(window, 'load', function() {
$$('a').each(function(e) {
e.writeAttribute('target', '_parent');
});
});
</script>
这是带有内容的IFrame内部。它最终成为该任务的最简单的解决方案。
That's inside the IFrame with the content. It ended up being the most simple solution for the task.
推荐答案
使用它来创建它,你可以访问任何具有$ body变量的部分:
Use this to create it and you'll have access to any parts with the $body variable:
$(function() {
var $frame = $('<iframe style="width:200px; height:100px;">');
$('body').html( $frame );
setTimeout( function() {
var doc = $frame[0].contentWindow.document;
var $body = $('body',doc);
$body.html('<h1>Test</h1>');
}, 1 );
});
所以你可以做这样的事情
So you can then do something like this
$('a', $body).attr('target', '_parent');
点击此处: http://groups.google.com/group/jquery-en/browse_thread/thread/fb646741a6192540
这篇关于强制IFrame中的任何HREF使用其父级作为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!