2011-07-08 35 views
1

我試圖添加一些Google網站優化器代碼前面的一段HTML和部分後面的代碼。問題在於,由於該網站是在編輯器中編輯的,因此我無法直接提示它,並且用於放置代碼/編輯html的選項非常有限。jquery:添加代碼(沒有直接編輯選項)的HTML部分(帶ID)

我只能在頁眉和頁面的按鈕中添加代碼/腳本。

我想impliment:

<script>utmx_section("Buybutton")</script> 

在面前:

<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> 

但它不起作用...

有人看到錯誤嗎? /看到另一個解決方案!?

更新的版本?:

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> 

它不會使鉻控制檯的任何錯誤 - 但它不會在谷歌網站優化驗證。

有人看到錯誤嗎? - 或者可以找出更簡單的解決方案?

在此先感謝!

  • 安德斯
+0

什麼東西出現在您的JavaScript控制檯(在Chrome/Firefox的shift-ctrl-J)?如果此代碼導致錯誤,他們將在那裏記錄。 –

+0

我得到: 「未捕獲的SyntaxError:意外的令牌非法」 – HolM

回答

0

試試這個:

$(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 />'); 

}); 

編輯:這不一定會緊跟在表,但這表之後放置關閉腳本標記中相同的容器內:

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

嗯...我仍然得到一個「未捕獲的SyntaxError:意外的令牌非法」...它看起來像不讀「「-code當我看着代碼的顏色在chrome SHIFT + CTRL + J中。 – HolM

+0

這不會在chrome中產生任何錯誤:SHIFT + CTRL + J,但它會使整個html部分消失。 – HolM

+0

請注意,它只有一個「結束」 -tag是允許的。 – HolM