1

这是我的代码:

<script>
document.getElementById(div').innerHTML = '<a href="javascript:void(0);" onclick="openPhpFile (\'asdasD\\Asdeqw.txt\');">efff</a>';
</script>

openPhpFile函数运行时,我提醒文件名,并且 \ 字符消失了,即使它们是双倍的。 addslashes()没有帮助;会是什么?

4

5 回答 5

3

你应该这样做:

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

请记住,如果您echo在双引号内使用 from PHP,您实际上需要4 个反斜杠。这是因为 PHP 也会使用双反斜杠序列,并且只会输出一个。因此,如果您希望 PHP 回显 2 个反斜杠,则需要输入 4。

于 2011-11-17T15:09:35.390 回答
2

尝试:

var div = document.getElementById("div");

div.innerHTML = '<a>efff</a>';

div.firstChild.onclick = function () {
  openPhpFile('asdasD\\\\Asdeqw.txt');
};
于 2011-11-17T15:18:27.007 回答
1

如果你打开 js 控制台,你会看到它被转为asdasD\Asdeqw.txt

所以尝试添加另一个斜线。

'<a href="javascript:void(0);" onclick="openPhpFile (\'asdasD\\\Asdeqw.txt\');">efff</a>'
于 2011-11-17T15:06:08.360 回答
1

您是否尝试过为每个反斜杠放置 4 个而不是 2 个或 3 个?

于 2011-11-17T15:09:51.000 回答
1

只是想知道为什么在这里需要反斜杠?不是所有操作系统都支持(甚至更喜欢)正斜杠吗?也许我在 Linux 世界里待得太久了。

我只会使用正斜杠,至少对于您的双反斜杠(显然不是引号)。我很想知道你在做什么,这意味着正斜杠不起作用。

于 2011-11-17T15:12:07.133 回答