如何使用js将<svg>标记更改为</svg> <img/>标记?

时间:2015-04-09 05:37:10

标签: javascript html image svg google-chrome-extension

我正在编写一个Chrome扩展程序来为朋友更改某个网站的徽标。问题是徽标是<svg>标记。如何更改js中的HTML标记和内容?

1 个答案:

答案 0 :(得分:1)

不确定是否允许使用jQuery,但您可以使用replaceWith();

$(document).ready(function(e) {
   $('svg').replaceWith('<img src="http://images.all-free-download.com/images/graphiclarge/smiley_10_55841.jpg" alt="smiley!"/>'); 
});
svg {
    width: 200px;
    height:200px;
}

img {
    width: 200px;
    height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <path d="M30,1h40l29,29v40l-29,29h-40l-29-29v-40z" stroke="#000" fill="none"/> 
  <path d="M31,3h38l28,28v38l-28,28h-38l-28-28v-38z" fill="#a23"/> 
  <text x="50" y="68" font-size="48" fill="#FFF" text-anchor="middle"><![CDATA[410]]></text>
</svg>
</div>

如果您只能使用JavaScript,则会有replaceChild个功能。

相关问题