JavaScript 和成为&;
我在 div 中有一些 javascript,使其从 iframe 移动到主页。它工作正常,但是当我使用 &像这样:
function JS_onMouseOverBG(e, i_bg) {
if( (block == 0)&&(i_bg != selected) ) {
e.style.cursor = 'pointer' ;
} else {
e.style.cursor = '' ;
}
}
然后 &
变成 &
并且我遇到了这个 firefox 错误:
Erreur : missing ) after condition
Fichier Source : http://192.168.0.20/lb/?p=467&w=prefs_bggen
Ligne : 905, Colonne : 22
Code Source :
if( (block == 0)&&(i_bg != selected) ) {
我尝试使用 的 unicode
但它不起作用。我有一个非法字符错误。\u0026
&
在这种情况下我该如何使用 &
?
编辑: 如果有人寻找同样的问题,我已经找到了解决方案:
function JS_decode(str) {
var div = document.createElement('div');
div.innerHTML = str ;
var decoded = div.firstChild.nodeValue;
return decoded ;
}
我将此函数用于包含 Javascript 代码的 DIV DIV_cover_js:
var eleJS = document.createElement("script") ;
eleJS.type = 'text/javascript' ;
eleJS.text = get('DIV_cover_js').innerHTML ;
eleJS.text = JS_decode(WP.eleJS.text) ;
eval(eleJS.text);
I have some javascript in a div to make it move from an iframe to the main page. It works fine but when I use & like this:
function JS_onMouseOverBG(e, i_bg) {
if( (block == 0)&&(i_bg != selected) ) {
e.style.cursor = 'pointer' ;
} else {
e.style.cursor = '' ;
}
}
Then &
becomes &
and I have this firefox error:
Erreur : missing ) after condition
Fichier Source : http://192.168.0.20/lb/?p=467&w=prefs_bggen
Ligne : 905, Colonne : 22
Code Source :
if( (block == 0)&&(i_bg != selected) ) {
I tried using the unicode \u0026
of &
but it does not work. I have an error of illegal character.
How can I do to use &
in such a case ?
Edit:
I've found the solution, if someone looks for the same issue:
function JS_decode(str) {
var div = document.createElement('div');
div.innerHTML = str ;
var decoded = div.firstChild.nodeValue;
return decoded ;
}
And I use this function for this DIV DIV_cover_js containing the Javascript code:
var eleJS = document.createElement("script") ;
eleJS.type = 'text/javascript' ;
eleJS.text = get('DIV_cover_js').innerHTML ;
eleJS.text = JS_decode(WP.eleJS.text) ;
eval(eleJS.text);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(引自OP的评论):
如果原始 HTML 包含
&&
那么那就是 错误,浏览器正在纠正它。然后,您将 DOM 序列化为 HTML,并尝试将某些 HTML 视为 JavaScript。
如果你真的想这样做,那么你应该提取一个
textNode
并使用它的data
而不是innerHTML
,但是这种方法很丑陋,速度很慢并且很难调试。首先,您应该通过元素加载正确的 JS 函数。
(Quoted from a comment by the OP):
If the original HTML includes
&&
then that is an error and the browser is correcting it.You then get a serialisation of the DOM to HTML and try to treat some HTML as JavaScript.
If you really want to do this, then you should extract a
textNode
and use itsdata
instead ofinnerHTML
, however the approach is ugly, slow and hard to debug. You should have a proper JS function loaded via a<script>
element in the first place.JavaScript 不会出现在
中。它位于
中。
JavaScript doesn't go in a
<div>
. It goes in a<script>
.