| prettify内のプログラムをコピーする処理 |
| prettify内のプログラムをコピーするサンプルです。 |
| [サンプル] |
| copyボタンをタップするとプログラムがクリップボードにコピーされた状態になります。 |
let result = 0;
result = random(0);
console.log(result);
function random(num) {
return Math.floor(Math.random() * num);
}
copy let message = "Hello, world!"; console.log(message);
.prettyprint ol.linenums > li {
list-style-type: decimal;
}
/* お好みでボタンのスタイルも設定 */
.copy-button {
display: inline-block; /* インライン要素をブロック要素のように扱う */
padding: 2px 5px; /* リンクテキストの周りに余白を追加 */
border: 1px solid #000; /* 枠線を追加 */
border-radius: 3px; /* 角を丸くする */
text-decoration: none; /* デフォルトの下線を消す */
color: #000; /* リンクの色 */
background-color: #ffffcc; /* 背景色を黄色に */
/* 蛍光ペン風の背景色 */
/* background: linear-gradient(transparent 60%, #ffffcc 60%); */
}
.copy-button:hover {
background-color: #ffeb99; /* ホバー時の背景色 */
}
<a href="javascript:void(0);" id="copy1" class="copy-button" data-target="codeBlock1">copy</a>a hrefタグのidはプログラムの処理としては使用されていませんが、
// Clipboard API を利用したコピー処理
function copyCodeById(targetId) {
const codeElement = document.getElementById(targetId);
if (!codeElement) {
console.error("対象のコードブロックが見つかりません: " + targetId);
return;
}
// コードブロック内のテキストを取得(タグ内の書式設定が不要な場合は innerText でOK)
const codeText = codeElement.innerText;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(codeText)
.then(() => {
console.log("コピー成功");
})
.catch(err => {
console.error("コピー中にエラーが発生しました:", err);
});
} else {
console.error("Clipboard API はこのブラウザで利用できません");
}
}
// 各コピーリンクに共通のイベントリスナーを設定
document.querySelectorAll('.copy-button').forEach(function(button) {
button.addEventListener('click', function() {
const targetId = this.getAttribute('data-target');
copyCodeById(targetId);
});
});
| Copying programs in prettify |
| This is an example of copying a program in prettify. |
| [sample] |
| When you tap the copy button, the program will be copied to the clipboard. |
let result = 0;
result = random(0);
console.log(result);
function random(num) {
return Math.floor(Math.random() * num);
}
copy let message = "Hello, world!"; console.log(message);
.prettyprint ol.linenums > li {
list-style-type: decimal;
}
/* Style the buttons to your liking */
.copy-button {
display: inline-block; /* Treat inline elements like block elements */
padding: 2px 5px; /* Add space around link text */
border: 1px solid #000; /* Add Border */
border-radius: 3px; /* Rounded Corners */
text-decoration: none; /* Remove the default underline */
color: #000; /* Link Color */
background-color: #ffffcc; /* Yellow background color */
/* Highlighter-style background color */
/* background: linear-gradient(transparent 60%, #ffffcc 60%); */
}
.copy-button:hover {
background-color: #ffeb99; /* Background color on hover */
}
<a href="javascript:void(0);" id="copy1" class="copy-button" data-target="codeBlock1">copy</a>The id of the a href tag is not used for program processing, but may be required for future feature expansion or individual operations.
// Copy processing using the Clipboard API
function copyCodeById(targetId) {
const codeElement = document.getElementById(targetId);
if (!codeElement) {
console.error("Target code block not found: " + targetId);
return;
}
// Get the text inside a code block (innerText is OK if you don't need formatting inside tags)
const codeText = codeElement.innerText;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(codeText)
.then(() => {
console.log("Copy successful");
})
.catch(err => {
console.error("An error occurred while copying:", err);
});
} else {
console.error("The Clipboard API is not available in this browser.");
}
}
// Set a common event listener for each copy link
document.querySelectorAll('.copy-button').forEach(function(button) {
button.addEventListener('click', function() {
const targetId = this.getAttribute('data-target');
copyCodeById(targetId);
});
});
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |