我正在尝试使用以下代码附加<script>
节点
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$('body').append('<script>alert(\'foo\');</script>');
</script>
</body>
</html>
我希望代码
alert('foo');
要添加执行,但实际上,添加了以下字符串
');
这是怎么回事
最佳答案
您必须在此处正确转义斜杠字符,并对foo使用双引号:
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$('body').append('<script>alert("foo");<\/script>');
</script>
</body>
</html>