有什么办法可以用自动风格的词吗?
我想要创建一个代码,每次我写一个特定的词,如“火”和“冰”分别采取红色和蓝色,而不是在标记之间,如产卵,p等。这是可能的吗?
发布于 2018-03-30 15:14:52
您可以使用jquery来完成它:
举个例子。
var StringFire = 'Fire',
newStringFire = '<span>Fire</span>',
newTextFire = $('p').text().replace(RegExp(StringFire,"gi"),newStringFire);
$('p').html(newTextFire);
span{
color: red;
font-family: sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Fire and Ice. Launch Audio in a New Window. By Robert Frost. Some say the world will end in fire,. Some say in ice. From what I've tasted of desire. I hold with those who favor fire. But if it had to perish twice,. I think I know enough of hate. To say that for destruction ice. Is also great. And would suffice. n/a. More About this ...</p>
对于多个单词:
var text = $('p').html().replace(/Ice/gi, "<span class='ice'>Ice</span>");
$('p').html(text);
var text = $('p').html().replace(/Fire/gi, "<span class='fire'>Fire</span>");
$('p').html(text);
.ice {
color: blue;
font: sans-serif;
}
.fire {
color: red;
font: sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Fire and Ice. Launch Audio in a New Window. By Robert Frost. Some say the world will end in fire,. Some say in ice. From what I've tasted of desire. I hold with those who favor fire. But if it had to perish twice,. I think I know enough of hate. To say that for destruction ice. Is also great. And would suffice. n/a. More About this ...</p>
发布于 2018-03-30 14:51:56
或者用剧本?
$(document).ready(function(){
$('#searchfor').keyup(function(){
page = $('#all_text').text();
searchedText = $('#searchfor').val();
$("p:contains('"+searchedText+"')").css("color", "red");
});
});
https://stackoverflow.com/questions/49582370
复制