홈페이지에 특정 텍스트를 클릭했을때 해당 텍스트가 자동으로 복사되게 가능할까요?
고수님들 답변 부탁드려요.
|
답변 1개
채택된 답변
+20 포인트
2020-06-28 (일) 17:25:07
<script>
jQuery.fn.selectText = function(){
this.find('input').each(function() {
if($(this).prev().length == 0 || !$(this).prev().hasClass('p_copy')) {
$('<p class="p_copy" style="position: absolute; z-index: -1;"></p>').insertBefore($(this));
}
$(this).prev().html($(this).val());
});
var doc = document;
var element = this[0];
console.log(this, element);
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
document.execCommand("copy");
};
// 아래부분 수정하세요
$(".bar").selectText();
</script>
답변을 작성하려면 로그인이 필요합니다.