【问题标题】:Organizing multiple embed codes with jQuery使用 jQuery 组织多个嵌入代码
【发布时间】:2011-02-12 06:56:22
【问题描述】:

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

嵌入代码 #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 文件中以保持标记的干净?

感谢您的帮助!

【问题讨论】:

    标签: javascript jquery embed organized


    【解决方案1】:

    我认为 jQuery 没有多大帮助 我要么直接 document.write 它们,要么将它们存储在一个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 2021-07-24
      • 2015-08-04
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多