将src属性的特定部分替换为iframe元素

时间:2016-07-12 11:40:59

标签: javascript jquery html xml blogger

有没有办法将src属性的特定部分替换为iframe元素?

我正在处理谷歌博客中的这个iframe ...

<iframe allowtransparency='true' id='reactions-iframe' class='reactions-iframe' expr:src='data:post.reactionsUrl'  frameborder='0' name='reactions' scrolling='no'/>

当博客加载时,src属性变为这样......

src="https://www.blogger.com/blog-post-reactions.g?options=%5BLike,+Dislike%5D&textColor=%23000000#http://www.blog-name.com/post-name.html"

因此,当博客加载时,我想将部分textColor=%23000000#替换为textColor=%23ffffff#。我怎么能用javascript或jQuery做到这一点?

1 个答案:

答案 0 :(得分:1)

向您的iframe添加onLoad个事件:

<iframe allowtransparency='true' id='reactions-iframe' class='reactions-iframe' expr:src='data:post.reactionsUrl'  frameborder='0' name='reactions' scrolling='no' onLoad='changeSrc();'/>

changeSrc()函数中执行此代码以更改src值:

function changeSrc() {

    const frame = document.getElementById('reactions-iframe');

    frame.src = frame.src.replace('textColor=%23000000#', 'textColor=%23ffffff#');

}