2013-05-31 42 views
0

我试图用iframe中的图像替换“注销”文本(注释部分)。用图像替换Iframe中的文本

我已经试过几件事情,但我认为这是最接近于正确的:

$("#comment-editor").contents().find("Sign out").html("<img src="http://3.bp.blogspot.com/-sJ_cc0hh_jw/TtllFIQ-NSI/AAAAAAAAEM4/zJdS8K9rjZ4/s1600/index.gif" alt="Sign out" />"); 

#comment-editor是iframe的ID。 “退出”是我想要替换的文本,虽然它的即时HTML结构如下:

<span id="signoutLink"> 
    <span id="commentDash" style="display: none;">–</span> 
    <a target="_top" href="http://xxxxxx.blogspot.com/logout?d=http://www.blogger.com/logout-redirect.g?blogID%3D4240883138904129005%26postID%3D3175710312324737334">Sign out</a> 
</span> 

什么我错在这里做什么?

+5

可能试图绕过[同源策略(http://en.wikipedia.org/wiki/Same_origin_policy)。 – jmeas

+1

如果iframe源不是来自同一个域,那么您将无法更改内容 - 演示 - http://fiddle.jshell.net/s9p7a/3/ – coderplus

回答

0

试试这个:

<span id="signoutLink"> 
<span id="commentDash">–</span> 
<a target="_top" href="http://xxxxxx.blogspot.com/logout?d=http://www.blogger.com/logout-redirect.g?blogID%3D4240883138904129005%26postID%3D3175710312324737334" id="anchor" >Sign out</a> 

$("#anchor").text('').html('<img src="http://3.bp.blogspot.com/-sJ_cc0hh_jw/TtllFIQ-NSI/AAAAAAAAEM4/zJdS8K9rjZ4/s1600/index.gif" alt="Sign out" />'); 

我添加了一个ID为锚标记,清除它的文本,然后添加HTML到它。

这里有一个小提琴:http://jsfiddle.net/u6nQP/14/

+0

如果您出于某种原因不想添加id到锚标记, '$(“#signoutLink a”).text('')。html('Sign out');' 也会做这个把戏 – livewire1407