如何使用Javascript或jQuery突出显示文本的前10个字符?
我想突出显示文本的前10个字符,以及单词段落的颜色如何使用Javascript或jQuery突出显示文本的前10个字符?,javascript,jquery,Javascript,Jquery,我想突出显示文本的前10个字符,以及单词段落的颜色 <html> <body> This is a heading This is a paragraph.This is another paragraph. </body> </html> 将其视为您的HTML内容: <div id="contents"> This is a heading This is a paragraph.This is anot
<html>
<body>
This is a heading
This is a paragraph.This is another paragraph.
</body>
</html>
将其视为您的HTML内容:
<div id="contents">
This is a heading
This is a paragraph.This is another paragraph.
</div>
此jQuery代码将执行您所需的操作:
$(document).ready(function() {
var data = $('#contents').text().trim();
var str = "";
for(var i = 0; i < 10; i++) {
str += data[i];
}
data = data.replace( /paragraph/g, '<span style="font-weight: bold;">paragraph</span>' );
data = data.replace( str, '<span style="background-color: yellow;">' + str + '</span>' );
$('#contents').html( data );
});
将其视为您的HTML内容:
<div id="contents">
This is a heading
This is a paragraph.This is another paragraph.
</div>
此jQuery代码将执行您所需的操作:
$(document).ready(function() {
var data = $('#contents').text().trim();
var str = "";
for(var i = 0; i < 10; i++) {
str += data[i];
}
data = data.replace( /paragraph/g, '<span style="font-weight: bold;">paragraph</span>' );
data = data.replace( str, '<span style="background-color: yellow;">' + str + '</span>' );
$('#contents').html( data );
});
在JS中尝试这段代码
<!DOCTYPE html>
<html>
<body>
<p>Click the button to highlight.</p>
<p id="demo">This is a heading. This is a paragraph. This is another paragraph.</p>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction()
{
var str= document.getElementById("demo").innerHTML;
var str1 = "";
for(var i = 0; i < 10; i++) {
str1 += str[i];
}
var n=str.replace(str1,'<span style="font-weight: bold;">' + str1 + '</span>');
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
在JS中尝试这段代码
<!DOCTYPE html>
<html>
<body>
<p>Click the button to highlight.</p>
<p id="demo">This is a heading. This is a paragraph. This is another paragraph.</p>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction()
{
var str= document.getElementById("demo").innerHTML;
var str1 = "";
for(var i = 0; i < 10; i++) {
str1 += str[i];
}
var n=str.replace(str1,'<span style="font-weight: bold;">' + str1 + '</span>');
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
What.Have.You.tryed.com空格/新行是否算作字符,或者是否需要解析这些字符以获取其他有效字符?ie:这是a还是这是hea?另外-突出显示是否有特殊要求?可以在JS中完成吗?是否需要在服务器端完成?你能把它写在适当的地方吗?因为你可以这样做:这是一个空格,数出10个字符,然后编辑.highlight CSS属性,或者只使用html5标记。@Rajeev我的答案对你有帮助吗?空格/新行按字符计算。这是在客户端完成的,实际上就像在加载文档后,我必须标记前10个字符。我还需要更改突出显示部分中任何单词的颜色what.Have.You.tryed.comDo空格/新行是否算作字符,或者是否需要将它们解析出来,以获取其他有效字符?ie:这是a还是这是hea?另外-突出显示是否有特殊要求?可以在JS中完成吗?是否需要在服务器端完成?你能把它写在适当的地方吗?因为你可以这样做:这是一个空格,数出10个字符,然后编辑.highlight CSS属性,或者只使用html5标记。@Rajeev我的答案对你有帮助吗?空格/新行按字符计算。这是在客户端完成的,实际上就像在加载文档后,我必须标记前10个字符。另外,我需要改变突出显示部分中任何单词的颜色。这只是一个快速回复。当然你们可以改进。我想改变单词的颜色,它只在高亮显示的区域,而不是一个未高亮显示的区域area@MuhammadAhmadZafar当我试图给突出显示区域中出现的单词涂上颜色时,突出显示效果消失了。您的问题中没有单词。你到底在说什么?所有要求。如果您有其他问题,请发布一个新问题,并将此问题标记为已回答。这只是一个快速回答。当然你们可以改进。我想改变单词的颜色,它只在高亮显示的区域,而不是一个未高亮显示的区域area@MuhammadAhmadZafar当我试图给突出显示区域中出现的单词涂上颜色时,突出显示效果消失了。您的问题中没有单词。你到底在说什么?所有要求。如果您有其他问题,请发布一个新问题,并将此问题标记为回答您错过了段落部分。但至少尝试了不同的逻辑。你错过了段落部分。但至少尝试了不同的逻辑。