0

次のスニペットを使用しようとしています。

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

まったく表示されていません。引用符の違いをたくさん試しましたが、わかりません。

4

2 に答える 2

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;">');​
于 2012-06-08T19:51:18.510 に答える
2

prepend()関数の文字列は、一重引用符で区切られます。つまり、その文字列内に入力するすべての引用符は、。でエスケープする必要があります。しかし、これはあなたの問題ではありません。あなたの問題は無効な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)を作成し、その文字列を変数として渡すことです。

于 2012-06-08T20:00:44.140 に答える