我有一个函数可以使一些文本段落变粗。它从div中随机选择单词并使它们变为粗体。我想要具有相同的功能,但不应该使文本变粗,而应该使单词加下划线。
最终结果应该是某些单词是粗体,有些单词加下划线。
这是我的函数,使随机单词变为粗体。
$(document).ready(function() {
$(function() {
function bold() {
var target = document.getElementById("randomText");
var oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
target = document.getElementById("randomText");
oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
words = oldHTML.split(" ");
wordCount = words.length;
var newHTML = "";
var highlightStart = Math.floor(Math.random() * wordCount) + 1;
var highlightLength = Math.floor(Math.random() * 5) + 1;
for (i = 0; i < wordCount; i++) {
if (i == highlightStart) newHTML = newHTML + "<b>";
newHTML = newHTML + words[i] + " ";
if (i == highlightStart + highlightLength) newHTML = newHTML + "</b>";
target.innerHTML = newHTML;
}
oldHTML = newHTML;
}
};
};
$('#randomText').load("hier.txt", bold)
})
});
我的想法是复制功能,并改变一些事情:
$(function() {
function underline() {
var target = document.getElementById("randomText");
var oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
target = document.getElementById("randomText");
oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
words = oldHTML.split(" ");
wordCount = words.length;
var newHTML = "";
var highlightStart = Math.floor(Math.random() * wordCount) + 1;
var highlightLength = Math.floor(Math.random() * 5) + 1;
for (i = 0; i < wordCount; i++) {
if (i == highlightStart) newHTML = newHTML + "<u>";
newHTML = newHTML + words[i] + " ";
if (i == highlightStart + highlightLength) newHTML = newHTML + "</u>";
target.innerHTML = newHTML;
}
oldHTML = newHTML;
}
};
};
$('#randomText').load("hier.txt", underline)
})
知道为什么这不起作用吗?
感谢。
答案 0 :(得分:0)
有一种更简单的方法可以获得粗体或带下划线的文本,即execCommand
函数
例如:
<body>
<button onclick='bold()'>Bold</button>
<button onclick='underline()'>Underline</button>
</body>
的Javascript:
function Bold(){
richTextField.document.execCommand('bold',false,null);
}
function Underline(){
richTextField.document.execCommand('underline',false,null);
}
您可以详细了解here。
答案 1 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE = edge,chrome = 1">
<title>Hello World </title>
</head>
<body>
<div id="randomText">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
function bold() {
var target = document.getElementById("randomText");
var oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
target = document.getElementById("randomText");
oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
words = oldHTML.split(" ");
wordCount = words.length;
var newHTML = "";
var highlightStart = Math.floor(Math.random() * wordCount) + 1;
var highlightLength = Math.floor(Math.random() * 5) + 1;
for (i = 0; i < wordCount; i++) {
if (i == highlightStart) newHTML = newHTML + "<b>";
newHTML = newHTML + words[i] + " ";
if (i == highlightStart + highlightLength) newHTML = newHTML + "</b>";
target.innerHTML = newHTML;
}
oldHTML = newHTML;
}
};
};
function underline() {
var target = document.getElementById("randomText");
var oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
target = document.getElementById("randomText");
oldHTML = target.innerHTML;
for (j = 0; j < 25; j++) {
words = oldHTML.split(" ");
wordCount = words.length;
var newHTML = "";
var highlightStart = Math.floor(Math.random() * wordCount) + 1;
var highlightLength = Math.floor(Math.random() * 5) + 1;
for (i = 0; i < wordCount; i++) {
if (i == highlightStart) newHTML = newHTML + "<u>";
newHTML = newHTML + words[i] + " ";
if (i == highlightStart + highlightLength) newHTML = newHTML + "</u>";
target.innerHTML = newHTML;
}
oldHTML = newHTML;
}
};
};
$('#randomText').load("hier.txt", bold);
$('#randomText').load("hier.txt", underline);
});
</script>
</body>
</html>