Auto Highlight Teks di dalam Tag PRE dengan jQuery
Posting ini sudah kadaluarsa. Mengapa?
Sebenarnya ini tidak mutlak pada elemen <pre>
. Hanya karena selektornya saja yang berupa $('pre')
jadi fungsi seleksi otomatis ini diberlakukan untuk elemen <pre>
:
$(function() {
$('pre').click(function() {
var refNode = $(this)[0];
if($.browser.msie) {
var range = document.body.createTextRange();
range.moveToElementText(refNode);
range.select();
} else if($.browser.mozilla || $.browser.opera) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(refNode);
selection.removeAllRanges();
selection.addRange(range);
} else if($.browser.safari) {
var selection = window.getSelection();
selection.setBaseAndExtent(refNode, 0, refNode, 1);
}
});
});
Sumber: Magp.ie
13 Comments:
kalo mau kayak blog nya ini gmn mas?? ada title nya klik ganda utk menyeleksi semua kode?? :D
By Putra, at Thursday, March 15, 2012 at 2:47:00 PM GMT+7
@Alam Perwira Itu cuma pake JQuery .attr() saja kok :)
$(function() {
$('pre').attr('title', 'Klik untuk menyeleksi semua kode!').click(function() {
var refNode = $(this)[0];
if($.browser.msie) {
var range = document.body.createTextRange();
range.moveToElementText(refNode);
range.select();
} else if($.browser.mozilla || $.browser.opera) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(refNode);
selection.removeAllRanges();
selection.addRange(range);
} else if($.browser.safari) {
var selection = window.getSelection();
selection.setBaseAndExtent(refNode, 0, refNode, 1);
}
});
});
By Taufik Nurrohman, at Thursday, March 15, 2012 at 8:17:00 PM GMT+7
untuk pre code ada ga? yang buat blogger
By Dody FariaL, at Saturday, March 24, 2012 at 3:22:00 AM GMT+7
@Dody FariaL Sama saja kok. Saya juga memakai <pre><code> ...
By Taufik Nurrohman, at Saturday, March 24, 2012 at 3:18:00 PM GMT+7
udah sya pasang yang diatas ^^^
tapi kok gak mau ya.... :p
By Anonymous, at Sunday, September 16, 2012 at 7:36:00 PM GMT+7
mas itu kodenya di taro dimana ya mas maklum masih newbie :(
By Anonymous, at Monday, September 17, 2012 at 8:22:00 PM GMT+7
Di dalam tag <script> :W
By Taufik Nurrohman, at Wednesday, September 19, 2012 at 12:18:00 PM GMT+7
tipe script nya java kan om.....
By dfdsh, at Sunday, January 13, 2013 at 8:50:00 PM GMT+7
JavaScript. Bukan Java.
By Taufik Nurrohman, at Monday, January 14, 2013 at 10:51:00 PM GMT+7
So how to double click to highlight as your own?
By Unknown, at Friday, July 26, 2013 at 7:27:00 PM GMT+7
It's already discussed completely ⇒ here
By Taufik Nurrohman, at Friday, July 26, 2013 at 8:24:00 PM GMT+7
Thanks but how to use of your code?
pre[i].ondblclick = function() {
autoSelect(this);
};
$('pre').on("dblclick", function() {
autoSelect(this);
});
By Unknown, at Saturday, July 27, 2013 at 9:09:00 PM GMT+7
klo blm bekerja coba deh tambahin kode berikut:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' type='text/javascript'/></script>
By Ali Zain Syahab, at Wednesday, August 7, 2013 at 5:10:00 PM GMT+7
Post a Comment
<< Home