防止选择在Firefox中的iframe中变灰而不使用contenteditable [英] Prevent selection being greyed out in iframe in Firefox without using contenteditable
问题描述
请参阅: http:// jsfiddle .net / 97Vjz / 8 /
看来只有生成的内容有这个问题,所以你可以制作一个页面(php / asp(x))生成内容为您规避问题。
使用javascript生成内容的另一个解决方案是加载它与 src =javascript:< ; html />'
(实际上这是Tim自己的解决方案,来自下面的评论。)
一个简单的脚本示例: http://jsfiddle.net/97Vjz/9/
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:
This unfortunately prevents styling the selection within an iframe using the ::-moz-selection
CSS pseudo-element because it only applies to non-grey selections:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!