javascript - Prevent selection being greyed out in iframe in Firefox without using contenteditable - Stack Overflow

In Firefox 3 and later (and probably older versions), selecting content within an iframe always seems t

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.

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

Share Improve this question edited Jul 20, 2011 at 11:51 Tim Down asked Jul 18, 2011 at 9:20 Tim DownTim Down 325k76 gold badges460 silver badges542 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7 +300

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/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 ments below.)

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

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

There is a property of the iframe exposed in Firebug's DOM inspector contentDocument->designMode which is set to false for you iFrames. Forcing it to true through the DOM inspector enables the blue highlight you're after.

Hypothesis: It seems that for dynamically written iFrames, either a XUL Iframe is rendered or the Gecko engine doesn't honor the styles.

Short of submitting a bug, the only workaround I can see is to wrap our contents in a textarea and style it to make it 'invisible': http://jsfiddle/mrchief/YYXSY/19/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743748953a4500537.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信