admin管理员组文章数量:1399991
I'm trying to auto-highlight the text of a <pre>
so that it's easier to copy... Here's what I've been working with:
jQuery( document ).ready( function() {
$( 'pre' ).click( function() {
$( this ).select();
var doc = document
, text = $( this )
, range, selection;
if( doc.body.createTextRange ) {
range = document.body.createTextRange();
range.moveToElementText( text );
range.select();
} else if( window.getSelection ) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents( text );
selection.removeAllRanges();
selection.addRange( range );
}
} );
} );
pre {cursor:pointer;}
<script src=".1.1/jquery.min.js"></script>
<pre>This is Text</pre>
I'm trying to auto-highlight the text of a <pre>
so that it's easier to copy... Here's what I've been working with:
jQuery( document ).ready( function() {
$( 'pre' ).click( function() {
$( this ).select();
var doc = document
, text = $( this )
, range, selection;
if( doc.body.createTextRange ) {
range = document.body.createTextRange();
range.moveToElementText( text );
range.select();
} else if( window.getSelection ) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents( text );
selection.removeAllRanges();
selection.addRange( range );
}
} );
} );
pre {cursor:pointer;}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>This is Text</pre>
The posts I've searched all refer to "highlighting" as a background color but I want to actually highlight it so the user can copy it easily. How can I modify the JS above so that when the user clicks on the text it highlights it?
Share Improve this question asked Jun 26, 2015 at 18:56 Howdy_McGeeHowdy_McGee 10.7k30 gold badges116 silver badges191 bronze badges 3- Uncaught TypeError: Failed to execute 'selectNodeContents' on 'Range': parameter 1 is not of type 'Node'. – user4639281 Commented Jun 26, 2015 at 18:59
- I don't get that issue in my console, I may have fudged the pasting, 1s. – Howdy_McGee Commented Jun 26, 2015 at 19:00
- You could also use a textarea which will also preserve white-space like this jsfiddle/dt8dzq4o much simpler – user4639281 Commented Jun 26, 2015 at 19:04
1 Answer
Reset to default 9Your code is pretty much spot-on. There's just one little change that needs to be made.
text = $(this)
needs to bee
text = this
The functions you use text
as a parameter to are Vanilla JavaScript methods, and so expect a DOM node rather than a jQuery object. "This" by itself in this case is a DOM node. But, wrapping it in $() turns it into a jQuery object, which is then unusable by the functions you call later on.
jQuery( document ).ready( function() {
$( 'pre' ).click( function() {
$( this ).select();
var doc = document
, text = this
, range, selection;
if( doc.body.createTextRange ) {
range = document.body.createTextRange();
range.moveToElementText( text );
range.select();
} else if( window.getSelection ) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents( text );
selection.removeAllRanges();
selection.addRange( range );
}
} );
} );
pre {cursor:pointer;}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>This is Text</pre>
本文标签: javascriptJQuery Highlight Inner Text of DivOnClickStack Overflow
版权声明:本文标题:javascript - JQuery Highlight Inner Text of Div, OnClick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230557a2596310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论