Đoạn mã JavaScript để thêm ký tự đặc biệt vào đầu mỗi dòng trong văn bản nhiều dòng (ok)
Bản nâng cấp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.login-form {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f2f2f2;
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<div style=" width: 800px; margin: 0 auto; ">
<div class="form-group">
<label for="specialChar">specialChar:</label>
<input type="text" id="specialChar" name="specialChar">
</div>
<div class="form-group">
<label for="originalText">originalText:</label>
<textarea id="originalText" name="note" rows="10"></textarea>
</div>
<pre id="result"></pre>
<button id="getmodifiedText" type="submit" style=" user-select: none; ">Submit</button>
</div>
<script>
function addSpecialCharToLines(text, specialChar) {
// Split the text into an array of lines
const lines = text.split('\n');
// Add the special character to the beginning of each line
var modifiedLines = lines.map(line => specialChar + " " + line.trim());
modifiedLines = modifiedLines.filter(function (el) {
return el != specialChar + " ";
});
console.log(modifiedLines);
// Join the modified lines back into a single string
var modifiedText = modifiedLines.join('\n');
console.log(modifiedText);
return modifiedText;
}
document.getElementById("originalText").addEventListener("input", function () {
var originalText = document.getElementById("originalText").value;
var result = document.getElementById("result");
var specialChar = document.getElementById("specialChar").value;
if (specialChar == "") {
specialChar = "▪️";
}
const modifiedText = addSpecialCharToLines(originalText, specialChar);
result.innerText = modifiedText;
});
</script>
</body>
</html>
Bản gốc
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function addSpecialCharToLines(text, specialChar) {
// Split the text into an array of lines
const lines = text.split('\n');
// Add the special character to the beginning of each line
const modifiedLines = lines.map(line => specialChar + " " + line.trim());
modifiedLines.shift();
modifiedLines.pop();
// Join the modified lines back into a single string
const modifiedText = modifiedLines.join('\n');
return modifiedText;
}
// Example usage:
const originalText = `
This is the first line.
This is the second line.
This is the third line.
`;
const specialChar = '▪️';
const modifiedText = addSpecialCharToLines(originalText, specialChar);
console.log(modifiedText);
</script>
</body>
</html>
PreviousLấy vị trí người dùng với javascriptNextTạo ra font in đậm và copy đến website để sử dụng (ok)
Last updated