防止选择在Firefox中的iframe中变灰而不使用conte

防止选择在Firefox中的iframe中变灰而不使用conte

本文介绍了防止选择在Firefox中的iframe中变灰而不使用contenteditable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox 3及更高版本(也可能是旧版本)中,在iframe中选择内容似乎总是使用用于当前没有焦点的文档的灰色选择背景色,即使iframe确实具有焦点。我能找到的唯一例外是iframe中的内容是可编辑的。在其他浏览器中不是这种情况。下面是一个例子说明:





这不幸的是可以避免使用



我的问题是:是否可以防止在没有的情况下使用 contenteditable / designMode ?



UPDATE



这似乎只发生在动态编写的iframe上:使用单独的文件和 src 属性解决了这个问题。然而,我确实需要它来处理动态编写的iframe。

我只是试图重现与真正的页面作为iframe的内容,然后它就像你想要的:蓝色选择! (FF 5.0)



请参阅:

看来只有生成的内容有这个问题,所以你可以制作一个页面(php / asp(x))生成内容为您规避问题。

使用javascript生成内容的另一个解决方案是加载它与 src =javascript:< ; html />'(实际上这是Tim自己的解决方案,来自下面的评论。)

一个简单的脚本示例:

  iframe.src ='javascript:\'< html>< body>'+ content +'< / body>< / html> \''; 


In Firefox 3 and later (and probably older versions), selecting content within an iframe always seems to use the grey selection background colour used for a document that doesn't currently have focus, even if the iframe does have focus. The only exception I have been able to find is when the content within the iframe is editable. This is not the case in other browsers. Here's an example illustrating this:

http://jsfiddle.net/97Vjz/

This unfortunately prevents styling the selection within an iframe using the ::-moz-selection CSS pseudo-element because it only applies to non-grey selections:

http://jsfiddle.net/YYXSY/1/

My question is: is it possible to prevent an iframe's selection being grey in Firefox without using contenteditable / designMode?

UPDATE

This only seems to happen on dynamically written iframes: using a separate file and the src attribute solves the problem. However, I do need it to work with dynamically written iframes.

解决方案

I just tried to reproduce the problem with a "real" page as iframe content and then it works like you want: blue colored selection! (FF 5.0)

see: http://jsfiddle.net/97Vjz/8/

It seems only generated content has this problem, so you could make a page (php/asp(x)) that generates the content for you to circumvent the problem.

Another solution to use javascript generated content is to load it with src="javascript:'<html />'" (actually this is Tim's own solution from the comments below.)

A simple example script: http://jsfiddle.net/97Vjz/9/

 iframe.src='javascript:\'<html><body>' + content + '</body></html>\'';

这篇关于防止选择在Firefox中的iframe中变灰而不使用contenteditable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 11:37