We've noticed that IE7 has an odd behavor with code blocks posted on Stack Overflow. For example, this little code block:

我们注意到IE7有一个奇怪的行为,在Stack Overflow上发布了代码块。例如,这个小代码块:

public PageSizer(string href, int index)
{
    HRef = href;
    PageIndex = index;
}

Copy and pasted from IE7, ends up like this:

从IE7复制并粘贴,最终结果如下:

public PageSizer(string href, int index){    HRef = href;    PageIndex = index;    }

Not exactly what we had in mind.. the underlying HTML source actually looks fine; if you View Source, you'll see this:

不完全是我们想到的..基础HTML源实际上看起来很好;如果你查看源代码,你会看到:

<pre><code>public PageSizer(string href, int index)
{
    HRef = href;
    PageIndex = index;
}
</code></pre>

So what are we doing wrong? Why can't IE7 copy and paste this HTML in a rational way?

那么我们做错了什么?为什么IE7不能以合理的方式复制和粘贴这个HTML?

Update: this specifically has to do with <pre> <code> blocks that are being modified at runtime via JavaScript. The native HTML does render and copy correctly; it's the JavaScript modified version of that HTML which doesn't behave as expected. Note that copying and pasting into WordPad or Word works because IE is putting different content in the rich text clipboard compared to the plain text clipboard that Notepad gets its data from.

更新:这特别与在运行时通过JavaScript修改的

 块有关。本机HTML确实可以正确呈现和复制;它是该HTML的JavaScript修改版本,其行为不符合预期。请注意,复制和粘贴到写字板或Word中是有效的,因为与将记事本从中获取数据的纯文本剪贴板相比,IE在富文本剪贴板中放置了不同的内容。

7 个解决方案

#1


35

It seems that this is a known bug for IE6 and prettify.js has a workaround for it. Specifically it replaces the BR tags with '\r\n'.

似乎这是IE6的已知错误,而prettify.js有一个解决方法。具体来说,它用'\ r \ n'替换BR标签。

By modifying the check to allow for IE6 or 7 then the cut-and-paste will work correctly from IE7, but it will render with a newline followed by a space. By checking for IE7 and providing just a '\r' instead of a '\r\n' it will continue to cut-and-paste and render correctly.

通过修改检查以允许IE6或7,然后剪切和粘贴将从IE7正常工作,但它将使用换行符后跟空格进行渲染。通过检查IE7并仅提供'\ r'而不是'\ r \ n',它将继续进行剪切和粘贴并正确渲染。

Add this code to prettify.js:

将此代码添加到prettify.js:

function _pr_isIE7() {
  var isIE7 = navigator && navigator.userAgent &&
       /\bMSIE 7\./.test(navigator.userAgent);
  _pr_isIE7 = function () { return isIE7; };
  return isIE7;
}

and then modify the prettyPrint function as follows:

然后修改prettyPrint函数,如下所示:

   function prettyPrint(opt_whenDone) {
     var isIE6 = _pr_isIE6();
+    var isIE7 = _pr_isIE7();

...

...

-        if (isIE6 && cs.tagName === 'PRE') {
+        if ((isIE6 || isIE7) && cs.tagName === 'PRE') {
          var lineBreaks = cs.getElementsByTagName('br');
+         var newline;
+         if (isIE6) {
+           newline = '\r\n';
+         } else {
+           newline = '\r';
+         }
          for (var j = lineBreaks.length; --j >= 0;) {
            var lineBreak = lineBreaks[j];
            lineBreak.parentNode.replaceChild(
-               document.createTextNode('\r\n'), lineBreak);
+               document.createTextNode(newline), lineBreak);
          }

You can see a working example here.

你可以在这里看到一个有效的例子。

Note: I haven't tested the original workaround in IE6, so I'm guessing it renders without the space caused by the '\n' that is seen in IE7, otherwise the fix is simpler.

注意:我没有在IE6中测试过原来的解决方法,所以我猜它没有IE7中出现的'\ n'引起的空间渲染,否则修复更简单。

更多相关文章

  1. HTML显示日期时间代码 - [js 特效代码]
  2. 解决html代码中插入的图片在浏览器中不显示的办法
  3. HTML代码格式化工具
  4. j2ee的web项目,有最终的html代码(即f12看到的最终给用户浏览器展示
  5. 如果字符串包含html代码,如何用python检测?
  6. 【竞价网站绝技】根据访客ip,页面显示访客所属城市的html代码(借用
  7. jquery入门-$.each 数组操作与表单操作代码
  8. HTML H5之ASCII 代码转义字符集实体编号
  9. 为什么代码放到DW里运行,和用记事保存为HTML的结果会不一样??

随机推荐

  1. android蓝牙通信
  2. ubuntu 安装配置 JDK7和Android(安卓)Stu
  3. Android(安卓)TextUtils.isEmpty()的认知
  4. android intent flag
  5. Android(安卓)平台下使用 i2c-tools调试I
  6. Android BroadCastReciver学习笔记
  7. android的color值
  8. 《Android音频焦点机制深入理解》
  9. Android(安卓)修改状态栏颜色
  10. #Android Training# Animations