这是我的代码:
<script>
document.getElementById(div').innerHTML = '<a href="javascript:void(0);" onclick="openPhpFile (\'asdasD\\Asdeqw.txt\');">efff</a>';
</script>
当 openPhpFile
函数运行时,我提示文件名,并且\字符消失了,即使它们加倍了。 addslashes()
没有帮助;它会是什么?
最佳答案
你应该这样做:
<script type='text/javascript'>
(function () { // Closures are your friend
// Declare variables
var theDiv, theLink;
// Create the link and assign attributes
theLink = document.createElement('a');
theLink.innerHTML = 'efff';
theLink.href = '#';
theLink.onclick = function () {
openPhpFile('asdasD\\Asdeqw.txt');
};
// Get a reference to the container, empty the container and add the link
theDiv = document.getElementById('div');
theDiv.innerHTML = '';
theDiv.appendChild(theLink);
})();
</script>
请记住,如果您在双引号内回显
来自 PHP 的内容,您实际上需要 4 个反斜杠。这是因为 PHP 也会使用双反斜杠序列,并且只会输出一个。所以如果你想让 PHP 回显 2 个反斜杠,你需要输入 4。
关于php - 反斜杠从文件名中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8169335/