php - 反斜杠从文件名中消失

标签 php javascript backslash slash

这是我的代码:

<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/

相关文章:

c++ - 在 Qt 中将 `\oct` 转换为 `char`

php - 通过 PHP 脚本发送推送通知

php - Laravel 中需要等效(不存在)whereHour 命令

javascript - 带有显示内联 block 或向左浮动的 jquery 垂直 slider

javascript - 如何关闭手机屏幕的AOS动画?

Ruby——寻找某种 "Regexp unescape"方法

PHP结束 session (不同的方式)我不明白

php - Laravel/Eloquent whereHas 与所有项目的关系?

javascript - 什么时候应该使用 call() 与直接调用函数?

C 预处理器 : line continuation: why exactly comment is not allowed after backslash character ('\' )?