我是Javascript和jQuery的新手。我在段落中有一个文本。我想用黄绿色突出显示以大写字母开头的所有单词。 这是我的源代码。但它不能正常运作。
<!DOCTYPE html>
<html>
<head>
<style>
.mycls {background-color:yellowgreen}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
var str = $(p).text();
words = str.split(' ');
for (var i = 0; i < words.length; i++) {
var w = words[i].split('');
if (w.charAt(0) === w.charAt(0).toUpperCase()) {
$(this).addClass("mycls");
}
// words[i] = letters.join('');
}
});
</script>
</head>
<body>
<p>President of USA Barack Obama is ...</p>
</body>
</html>
谢谢大家!
答案 0 :(得分:2)
...
Dim xpathToExtractRow As String, XMLNamespaces As String
Dim dom As DOMDocument60
xpathToExtractRow = "/doc:cqresponse/doc:rows/doc:row"
XMLNamespaces = "xmlns:doc='http://ibm.com/rational/cqweb/v7.1'"
Set dom = New DOMDocument60
dom.Load ("C:\ABC.xml")
dom.setProperty "SelectionNamespaces", XMLNamespaces
...
&#13;
$('p').each(function(){ // to each <p>
r = /[A-Z]\w*/g; // big letter with word symbols, global search
function f(x){
return '<span class="y">'+x+'</span>' // rewrited
}
h = $(this).html(); //get
h = h.replace(r,f); //replace
$(this).html(h); //set
}) //done
&#13;
.y {
background-color: yellowgreen;
}
&#13;
答案 1 :(得分:1)
试试这个:
<p>President of USA Barack Obama is ...</p>
<style>
.highlighted
{
background-color: yellow;
}
</style>
<script>
var split = $('p').text().split("");
var upperCase= new RegExp('[A-Z]');
$.each(split,function(i)
{
if(split[i].match(upperCase))
{
$('p').html($('p').html().replace(split[i],'<span class=\"highlighted\">' + split[i] + '</span>'));
}
});
</script>