我正在寻找与此(Java)代码等效的JavaScript:org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(String)
所以我可以在JavaScript中使用它,例如:
loop ... foo = something;
elem.setAttribute("onclick", "bar(event, 'this:" + used_here(foo) + "');");
foo = something else;
repeat loop
因此,这里缺少的是used_here()函数,该函数会根据JavaScript字符串规则正确地转义其参数。
是否存在标准功能(主要浏览器支持)?
-
假重复的解释:这与HTML转义无关。
So NOT THIS: " -> "
But this: " -> \"
答案 0 :(得分:2)
请原谅任何冒犯行为,但这就像询问如何为旋转电话实现地址簿应用程序或为鸽子作防箭装甲一样。 :)在当今时代,您几乎永远都没有理由在HTML属性中使用JavaScript作为字符串。
elem.setAttribute("onclick", "alert('this:" + used_here(foo) + "');");
这几天写得很清楚
let foo = `Chief O'Brien & Dwayne "The Rock" Johnson can now have any special chars they want!`;
let elem = document.querySelector('button');
elem.addEventListener('click', evt => {
alert(foo);
});
<button>Click me!</button>
编辑:循环。
let elems = document.querySelectorAll('button');
elems.forEach((elem, i) => {
let foo = `Button ${i + 1}`; // foo is different, but it is local
elem.addEventListener('click', evt => {
alert(foo);
});
});
<button>Click me!</button>
<button>No, click me!</button>
<button>No, me!</button>
或者,为了使用jQuery支持IE8,有意不使用.each,因为我想展示老式的for循环,其中必须防止foo
上的闭包:
var elems = $('button');
var i, elem, foo;
for (i = 0; i < elems.length; i++) {
elem = elems[i];
foo = "Button " + (i + 1);
(function(foo) {
$(elem).on('click', function(evt) {
alert(foo);
});
})(foo);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button>Click me!</button>
<button>No, click me!</button>
<button>No, me!</button>
答案 1 :(得分:0)
我通过escapeJavaStyleString
从org.apache.commons.lang.StringEscapeUtils.escapeJavaScript
转换为javascript:
function escapeJavaStyleString(str, escapeSingleQuote, escapeForwardSlash) {
if (str == null) {
return '';
}
const sz = str.length;
let out = '';
for (let i = 0; i < sz; i++) {
const ch = str.charAt(i);
// handle unicode
if (ch > 0xfff) {
out += "\\u" + ch.toString(16);
} else if (ch > 0xff) {
out += "\\u0" + ch.toString(16);
} else if (ch > 0x7f) {
out += "\\u00" + ch.toString(16);
} else if (ch < 32) {
switch (ch) {
case '\b':
out += '\\';
out += 'b';
break;
case '\n' :
out += '\\';
out += 'n';
break;
case '\t' :
out += '\\';
out += 't';
break;
case '\f' :
out += '\\';
out += 'f';
break;
case '\r' :
out += '\\';
out += 'r';
break;
default :
if (ch > 0xf) {
out += "\\u00" + ch.toString(16);
} else {
out += "\\u000" + ch.toString(16);
}
break;
}
} else {
switch (ch) {
case '\'' :
if (escapeSingleQuote) {
out += '\\';
}
out += '\'';
break;
case '"' :
out += '\\';
out += '"';
break;
case '\\' :
out += '\\';
out += '\\';
break;
case '/' :
if (escapeForwardSlash) {
out += '\\';
}
out += '/';
break;
default :
out += ch;
break;
}
}
}
return out;
}
console.log(escapeJavaStyleString('testing("bleh")'));
答案 2 :(得分:-3)
var d = `Chief O'Brien & Dwayne "The Rock" Johnson can now have any special chars they want!`
var used_here = function(foo){
return foo.replace(/'/g, "\\'");
}
var result = used_here(d)
document.body.setAttribute('onclick',"alert('"+result+"')")
html,body{
width:100%;
height:100%;
}