使用jQuery组织多个嵌入代码

时间:2010-04-26 07:11:18

标签: javascript jquery embed organized

我的网站上有几个嵌入代码,例如:

嵌入代码#1:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/f8Lp2ssd5A9ErAc&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/f8Lp2A9ErAc&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

嵌入代码#2:

<script type="text/javascript">
_qoptions={
qacct:"p-3asdb5E0g6"
};
</script>
<script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script>
<noscript>
<a href="http://www.quantcast.com/p-3asdb5E0g6" target="_blank"><img src="http://pixel.quantserve.com/pixel/p-3asdb5E0g6.gif" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/></a>
</noscript>

依旧......

如何组织它们并将它们分成外部单个js文件以保持标记清洁?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我没有看到jQuery有多大帮助 我会直接写文档或者将它们存储在xml文件中

使用document.write它会是这样的(删除noscript,因为在js文件中根本没有任何意义

// --- starts jsfile
var embeds = [
'<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/f8Lp2ssd5A9ErAc&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/f8Lp2A9ErAc&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>',

'<script type="text/javascript">_qoptions={qacct:"p-3asdb5E0g6"};</script><script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script>'
]; // notice the lack of comma on the last embed
function putEmbed(idx) {
  document.write(embeds[idx]);
}
// ------ end ------

然后使用

<script type="text/javascript">
putEmbed(0); // youtube
</script>

以后

<script type="text/javascript">
putEmbed(1); // quant
</script>