admin管理员组文章数量:1326112
I have some code block on my blog; I want that when someone double-click on a code block, all code of that code block need to be selected.
Please look at the code below (this is what I’ve got so far, though it is using jQuery). Now is it do-able with native JavaScript( without jQuery ) ?
Sorry If I've asked a dumb question, I’m new at these things. :)
<script type="text/javascript" src=".12.0.min.js"></script>
<style>
pre.highlight {
border: 1px solid #ccc;
padding: 10px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
// select all code on doubleclick
$('pre.highlight').dblclick(function() {
$(this).select();
var text = this,
range, selection;
if (document.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);
}
});
});
</script>
<pre class="highlight"><code>.button-css {
cursor: pointer;
border: none;
background: #F2861D;
padding: 3px 8px;
margin: 7px 0 0;
color: #f4f4f4;
}</code></pre>
<pre class="highlight"><code> #slider {
border-color: #c0c0c0;
border-radius: 5px 5px 5px 5px;
border-style: solid;
}</code></pre>
I have some code block on my blog; I want that when someone double-click on a code block, all code of that code block need to be selected.
Please look at the code below (this is what I’ve got so far, though it is using jQuery). Now is it do-able with native JavaScript( without jQuery ) ?
Sorry If I've asked a dumb question, I’m new at these things. :)
<script type="text/javascript" src="https://code.jquery./jquery-1.12.0.min.js"></script>
<style>
pre.highlight {
border: 1px solid #ccc;
padding: 10px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
// select all code on doubleclick
$('pre.highlight').dblclick(function() {
$(this).select();
var text = this,
range, selection;
if (document.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);
}
});
});
</script>
<pre class="highlight"><code>.button-css {
cursor: pointer;
border: none;
background: #F2861D;
padding: 3px 8px;
margin: 7px 0 0;
color: #f4f4f4;
}</code></pre>
<pre class="highlight"><code> #slider {
border-color: #c0c0c0;
border-radius: 5px 5px 5px 5px;
border-style: solid;
}</code></pre>
Share
Improve this question
asked Feb 9, 2016 at 17:06
Shuvojit DasShuvojit Das
1932 silver badges10 bronze badges
4
- Can you add sample HTML as well. A jsFiddle would be useful – Thangaraja Commented Feb 9, 2016 at 17:09
- do you want to get it with native javascript? – RomanPerekhrest Commented Feb 9, 2016 at 17:16
- You might find some useful code in my greasemonkey script. Full details at my stackapps post. – rojo Commented Feb 9, 2016 at 17:26
- Yes I want to achive that with native JavaScript @RomanPerekhrest – Shuvojit Das Commented Feb 9, 2016 at 17:30
4 Answers
Reset to default 4Your code works fine in Jquery.
To get the "native javascript" version go through the following steps:
- replace jquery's
$(document).ready
handler with nativewindow.onload
- work with event target
e.target
instead of jquery'sthis
instead of adding an event handler for each element with
class="highlight"
use advanced technic which is adding the event listener to the parent element once and considering only neededpre
orcode
elements (related toclass="highlight"
)window.onload = function(){ document.body.addEventListener('dblclick', function(e){ var target = e.target || e.srcElement; if (target.className.indexOf("highlight") !== -1 || target.parentNode.className.indexOf("highlight") !== -1){ var range, selection; if (document.body.createTextRange) { range = document.body.createTextRange(); range.moveToElementText(target); range.select(); } else if (window.getSelection) { selection = window.getSelection(); range = document.createRange(); range.selectNodeContents(target); selection.removeAllRanges(); selection.addRange(range); } e.stopPropagation(); } }); };
https://jsfiddle/8nba46x8/
Converting jQuery to JavaScript for your code is easy, you did the hard part already.
var srcBox = document.querySelector(".sourceBox");
srcBox.addEventListener("dblclick", hiLite, false);
- Wrap the content in another container (
.sourceBox
) because the most efficient way to handle multiple event listeners is to place the one event listener on the parent of all of theevent.target
s (the element where the event originated from, or simply the element that was actually clicked).
if (e.target !== e.currentTarget)
- Even though the
event.target
would be one of thepre
elements but the listener is on the parent, we can still find the correctpre
element by checking if it's not theevent.currentTarget
(.sourceBox
) in the event chain as it probigates (and stops short ofevent.currentTarget
) and bubbles (and stops short ofevent.currentTarget
). The normal event chain is stopped short because of thefalse
parameter of the event listener and thee.stopPropagation();
placed at the very end of the function hiLite (event handler)
var text = e.target;
- Singling out the correct
event.target
allows us to usethis
more or less. I believe thatthis
in this context is still.sourceBox
which in this situation is useless, which is why we're usingevent.target
As you can see this is one of the many reasons why many prefer jQuery over JavaScript. I'm a masochist so naturally I prefer JavaScript.
Now that my incoherent ramblings have thoroughly confused you, here's an article that explains it better than I can.
var srcBox = document.querySelector(".sourceBox");
srcBox.addEventListener("dblclick", hiLite, false);
function hiLite(e) {
if (e.target !== e.currentTarget) {
var text = e.target;
var range, selection;
if (document.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);
}
}
e.stopPropagation();
}
pre.highlight {
border: 1px solid #ccc;
padding: 10px;
}
.sourceBox {
border: 2px inset #222;
padding: 1px 15px;
}
<section class="sourceBox">
<pre class="highlight"><code>.button-css {
cursor: pointer;
border: none;
background: #F2861D;
padding: 3px 8px;
margin: 7px 0 0;
color: #f4f4f4;
}</code></pre>
<pre class="highlight"><code> #slider {
border-color: #c0c0c0;
border-radius: 5px 5px 5px 5px;
border-style: solid;
}</code></pre>
</section>
For pre or any tag one can select all text inside that tag by this simple code. It will highlight the entire tag area with yellow colour and select text inside it on single click.
document.onclick = function(event) {
var range, selection;
event.target.style.backgroundColor = 'yellow';
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(event.target);
selection.removeAllRanges();
selection.addRange(range);
};
Here is the code to implement the same functionality in native javascript :
<script type="text/javascript">
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
</script>
To use the above function you can write in HTML like this :
<div id="selectable" ondblclick="selectText('selectable')">This is a test div.</div>
本文标签: jqueryJavaScriptselect all text inside a pre code block’ on doubleclickStack Overflow
版权声明:本文标题:jquery - JavaScript - select all text inside a ‘pre code block’ on double-click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192408a2430457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论