Javascript jquery:在没有直接编辑选项的html部分(带有id)周围添加代码

Javascript jquery:在没有直接编辑选项的html部分(带有id)周围添加代码,javascript,jquery,html,Javascript,Jquery,Html,我试图在html的一部分前面添加一些Google网站优化器代码,在该部分后面添加一些代码。问题是我不能直接实现它,因为该站点是在编辑器中编辑的,并且输入代码/编辑html的选项非常有限 我只能在页眉和页面按钮上实现代码/脚本 我想实现: <script>utmx_section("Buybutton")</script> utmx\u部分(“购买按钮”) 在: <TABLE ID="BUYSECTION"> <TR> <TD ALIGN=

我试图在html的一部分前面添加一些Google网站优化器代码,在该部分后面添加一些代码。问题是我不能直接实现它,因为该站点是在编辑器中编辑的,并且输入代码/编辑html的选项非常有限

我只能在页眉和页面按钮上实现代码/脚本

我想实现:

<script>utmx_section("Buybutton")</script>
utmx\u部分(“购买按钮”)
在:

<TABLE ID="BUYSECTION">
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">Amount<BR><INPUT ID="amount" TYPE="TEXT" CLASS="TextInputField_ProductInfo" NAME="AMOUNT" SIZE="3" MAXLENGTH="6" VALUE="1"></TD>
<TD>&nbsp;&nbsp;</TD>
<TD CLASS="BuyButton_ProductInfo">Buy<BR><INPUT TYPE="IMAGE" BORDER="0" SRC="/images/buybutton.png"></TD>
</TR>
</TABLE>

金额
购买
然后是以下代码:

</noscript>

我在页面的按钮处尝试了以下代码(jquery):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script>
$('<script>utmx_section("Buybutton")</script>').insertBefore('#BUYSECTION');
$('</noscript>').insertAfter('#BUYSECTION');
</script>

$('utmx_节(“Buybutton”))。在('BUYSECTION')之前插入;
$(“”).insertAfter(“#购买部分”);
但它不起作用

有人看到错误了吗?/看到另一个解决方案了吗

更新:

AlienWebguy帮了很多忙。。。但它还没有完全起作用。我试图通过AlienWebguy所做的工作来修复代码,但这不起作用

到目前为止,我得到的是:

<script>
// Create script tag with native JS
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = eval('utmx_section("Buybutton")');

// Add it in front of the html-section with JQuery
$(script).insertBefore('#BUYSECTION');

// Add a </noscript>-end-tag  after the html-section
$('#BUYSECTION').parent().html($('#BUYSECTION').parent().html() + '</noscript>');
</script>

//使用本机JS创建脚本标记
var script=document.createElement('script');
script.type='text/javascript';
script.innerHTML=eval('utmx_节(“Buybutton”));
//使用JQuery将其添加到html部分前面
$(脚本).insertBefore(“#buysecute”);
//在html部分后添加-end标记
$('buysecute').parent().html($('buysecute').parent().html()+“”);
它不会在chrome控制台中出错,但不会在Google网站优化器中验证

有人看到错误了吗-或者能想出一个更简单的解决方案

提前谢谢

  • 安德斯
试试这个:

$(function(){

    // Create script tag with native JS
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.innerHTML = eval('utmx_section("Buybutton")');

    // Add it with JQuery
    $('head').append(script);

    // Wrap table with noscript
    $('#BUYSECTION').wrap('<noscript />');

});
$(函数(){
//使用本机JS创建脚本标记
var script=document.createElement('script');
script.type='text/javascript';
script.innerHTML=eval('utmx_节(“Buybutton”));
//用JQuery添加它
$('head').append(脚本);
//使用noscript包装表
$(“#购买部分”).wrap(“”);
});
编辑:它不一定紧跟在表后面,但这会将结束noscript标记放在同一容器中的表后面:

$('#BUYSECTION').parent().html($('#BUYSECTION').parent().html() + '</noscript>');
$('BUYSECTION').parent().html($('BUYSECTION').parent().html()+“”);

javascript控制台(chrome/firefox中的shift-ctrl-J)中是否显示了任何内容?如果此代码导致错误,它们将被记录在那里。我得到一个:“未捕获的SyntaxError:意外标记非法”嗯。。。我仍然得到一个“未捕获的SyntaxError:意外标记非法”。。。当我在chrome SHIFT+CTRL+J中查看代码的颜色时,它似乎不会“读取”“utmx_部分(“Buybutton”)中的第一个代码。这不会在chrome:SHIFT+CTRL+J中产生任何错误,但会使整个html部分消失。请注意,它只是一个允许的“结束”标记。utmx_部分(“Buybutton”)+HTML节+CTRL+SHIFT+I打开控制台。