首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Javascript插入HTML Javascript

将Javascript插入HTML Javascript
EN

Stack Overflow用户
提问于 2013-03-08 01:57:17
回答 1查看 895关注 0票数 0

我试图插入这个Javascript代码。谷歌:

代码语言:javascript
复制
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1362706866260-0'); });
</script>

到这个JavaScript / HTML代码中:

代码语言:javascript
复制
var theDivs = {
      "div1": makeSpecialBox("doublearticlewrapperadmp", 
              '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0">
              <div class="entry-image"></div></div>'),
      ...
};

我想在我的“输入图像”div之间插入上面列出的第一段代码。我试着这么做:

代码语言:javascript
复制
var theDivs = {
      "div1": makeSpecialBox("doublearticlewrapperadmp", 
'<div class="articlewrapper" id="div-gpt-ad-1362706866260-0">
<div class="entry-image">googletag.cmd.push(function() { googletag.display('div-gpt-ad-1362706866260-0'); });</div></div>'),

在我的javascript代码的HTML部分中添加来自Google的直接的javascript代码是不起作用的,不管有没有脚本标记 :(我将如何绕过这个问题并正确地将上述Google代码插入到我现有的代码中?

最新代码:

代码语言:javascript
复制
<script type="text/javascript">
$(document).ready(function () {
var $window = $(window);
var resized = false;
var resized500 = false;
var resized600 = false;
var resized700 = false;
var resized800 = false;

var sadfdsf = true;
var theDivs = {
        "div1": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"><div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</script></div></div>'),

        "div2": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'),

        "div3": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'),

        "div4": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'),

        "div5": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'),

function makeSpecialBox(className, content) {
    // Create a new special box with the class name and the content
    var specialBox = $("<div />").addClass(className).html(content);
    // Return the special box
    return specialBox;
}

function removeSpecialBoxes() {
    // Get the special boxes
    var specialBoxes = $("#wrapper div").filter("[class^=doublearticlewrapperadmp]");
    // Remove the special boxes
    specialBoxes.remove();
}

function setNthPosition(theDiv, newPos) {
    // Generate the special box
    var theClone = theDivs["div" + theDiv].clone();
    // Get the nth-child
    var nthChild = $("#wrapper > div:nth-of-type(" + newPos + ")");
    // Append the special box after the nth-child
    nthChild.after(theClone);
}

function checkWidth() {
    var windowsize = $window.width();
    if (windowsize >= 1442) {
        //if the window is in between the sizes of 440 and 500px
        //if(resized==false){
        // Remove special boxes
        removeSpecialBoxes();
        // Reposition
        setNthPosition(1, 9);
        setNthPosition(2, 15);
        setNthPosition(3, 29);
        setNthPosition(4, 35);
        resized = true;
        //  }
    }

    windowsize = $window.width();
    if (windowsize > 1278 && windowsize < 1441)  {
        //if the window is in between the sizes of 500 and 600px
        // if(resized500==false){
        // Remove special boxes
        removeSpecialBoxes();
        // Reposition
        setNthPosition(1, 7);
        setNthPosition(2, 16);
        setNthPosition(3, 31);
        setNthPosition(4, 40);
        resized500 = true;

        // }
    }

    if (windowsize < 1278) {
        //if the window is in between the sizes of 600 and 700px
        // if(resized600==false){
        // Remove special boxes
        removeSpecialBoxes();
        // Reposition
        setNthPosition(1, 5);
        setNthPosition(2, 15);
        setNthPosition(3, 29);
        setNthPosition(4, 39);
        resized600 = true;

        //}
    }
}

// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});

`

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-08 02:04:25

你忘了你的脚本标签了:

代码语言:javascript
复制
var theDivs = {
  "div1": makeSpecialBox("doublearticlewrapperadmp", 
  '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0">
  <div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</script></div></div>'),

也要注意使用单引号/双引号。有些可能也可能不需要转义(虽然似乎不需要.),但我确实在googletag.display("div-gpt-ad-1362706866260-0")部分中将单引号更改为双引号。

编辑:

您可能在这里找到您的解决方案:Script tag in JavaScript string

代码语言:javascript
复制
var test = '...... </scr'+'ipt>......';

这个小技巧可能可以绕过浏览器解析标记。

所以在你的情况下:

代码语言:javascript
复制
var theDivs = {
  "div1": makeSpecialBox("doublearticlewrapperadmp", 
  '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0">
  <div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</scr' + 'ipt></div></div>'),
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15285409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档