2012-06-08 25 views
0

我嘗試使用下面的代碼片段時逃逸引號的問題:jQuery的 - 前面加上內容

$('#thirdPartyCheckoutButtons').prepend('<a href="https://www.resellerratings.com" onclick="window.open("https://seals.resellerratings.com/landing.php?seller=myID","name","height=760,width=780,scrollbars=1"); return false;"><img style="border:none;" src="//seals.resellerratings.com/seal.php?seller=myID" oncontextmenu="alert("Copying Prohibited by Law - ResellerRatings seal is a Trademark of All Enthusiast, Inc."); return false;" /></a><img src="https://www.myurl.com/hdsd834k.png" style="float: left;margin-left: 180px;">'); 

它沒有顯示所有..我已經嘗試了許多不同的報價,我不得到它!

回答

2

內部內部雙引號打破了您的onclick屬性值。用單引號替換它們並轉義它們。

...onclick="window.open(\'https://seals.resellerratings.com/landing.php?seller=myID\',\'name\',\'height=760,width=780,scrollbars=1\'); return false;"... 

同樣的事情發生在你的oncontextmenu事件,執行相同的修復。

...oncontextmenu="alert(\'Copying Prohibited by Law - ResellerRatings seal is a Trademark of All Enthusiast, Inc.\'); return false;" />... 

全碼:

$('#thirdPartyCheckoutButtons').prepend('<a href="https://www.resellerratings.com" onclick="window.open(\'https://seals.resellerratings.com/landing.php?seller=myID\',\'name\',\'height=760,width=780,scrollbars=1\'); return false;"><img style="border:none;" src="//seals.resellerratings.com/seal.php?seller=myID" oncontextmenu="alert(\'Copying Prohibited by Law - ResellerRatings seal is a Trademark of All Enthusiast, Inc.\'); return false;" /></a><img src="https://www.myurl.com/hdsd834k.png" style="float: left;margin-left: 180px;">');​ 
+1

它是一個HTML屬性值; \毫無意義。 – Quentin

+0

啊,謝謝!!!!! – user1098767

+0

@Quentin正確,更新。 –

2

您在預定()函數的字符串由單引號分隔。這意味着您在該字符串中輸入的每個單引號都必須由a轉義。但是,這不是你的問題。你的問題在於無效的HTML。這是你擁有的一切:

$('#thirdPartyCheckoutButtons').prepend('<a href="https://www.resellerratings.com" onclick="window.open("https://seals.resellerratings.com/landing.php?seller=myID","name","height=760,width=780,scrollbars=1"); return false;"><img style="border:none;" src="//seals.resellerratings.com/seal.php?seller=myID" oncontextmenu="alert("Copying Prohibited by Law - ResellerRatings seal is a Trademark of All Enthusiast, Inc."); return false;" /></a><img src="https://www.myurl.com/hdsd834k.png" style="float: left;margin-left: 180px;">'); 

如果你發現所有的HTML的屬性值是由雙引號分隔。但是,在您的onclick =「」事件中,您再次使用雙引號。您可以使用ESCAPED單引號來糾正此衝突。你的oncontextmenu =「」事件也有同樣的問題。

$('#thirdPartyCheckoutButtons').prepend('<a href="https://www.resellerratings.com" onclick="window.open(\'https://seals.resellerratings.com/landing.php?seller=myID\',\'name\',\'height=760,width=780,scrollbars=1\'); return false;"><img style="border:none;" src="//seals.resellerratings.com/seal.php?seller=myID" oncontextmenu="alert(\'Copying Prohibited by Law - ResellerRatings seal is a Trademark of All Enthusiast, Inc.\'); return false;" /></a><img src="https://www.myurl.com/hdsd834k.png" style="float: left;margin-left: 180px;">'); 

,以避免在將來出現此問題最簡單的方法是建立jQuery函數調用外部串(HTML),然後通過在字符串中作爲一個變量。

+0

謝謝你 – user1098767