Javascript 替换文字功能

Javascript replace text function

这是我的 javascript 用于替换一些文本:

document.body.innerHTML = document.body.innerHTML.replace(/Sometext/g, 'difference');    

如何更改颜色、字体大小、字体等?

我需要像这样 link 我的脚本,不能使用 { 否则:

<script>$(document).ready($.getScript("url"));</script>   

我认为这样的方法可行:

window.onload = function() {
document.body.innerHTML =
        document.body.innerHTML.replace(/Deckling/g, result);
}

var str = "The Liberator";
var result =     str.fontcolor("Red").italics().fontsize(6);
result.style.fontFamily = "Harrington"; 

有什么帮助吗? (第一个 post 和非常有限的知识)

那行不通: var result = str.fontcolor("Red").italics().fontsize(6);。 您需要添加 css 以将 surface.Add 更改为您的 header:

  .textstyle{
     font-size:16px;
     font-family:Harrington;
  }
</style>

并将此添加到您的 window.onload:$('body').addClass('textstyle');

您可以将文本包装在 divspan 标签中,select 它在 JS 中应用 class.

class 将包含您的文本的样式。

只是一个普通的简单例子 javaScript(没有 jquery): http://jsbin.com/yufiteseme/1/

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    .a {
    color:red;
    font-style: italic;
    font-size: 6px;
    {
    </style>
     <script>
    function changeColor(){
        document.getElementById('text').classList.add('a');
    } 
    </script>
</head>
<body onload="changeColor()">
     <div id="text">
    Test for example
    </div>
</body>
</html>

您可以使用 javascript 更改 html 元素的样式,将脚本放在元素下方。

以下示例使用 javascript 更改 'p' 元素的样式:

<!DOCTYPE html>
<html>
<body>
    <p id="p1">Hello World!</p>
    <script>
        document.getElementById("p1").style.color = "red";
        document.getElementById("p1").style.fontFamily = "Arial";
        document.getElementById("p1").style.fontSize = "larger";
    </script>
</body>
</html>

您还可以使用 jquery 更改 html 元素的样式。

以下示例使用 jquery 更改 'p' 元素的样式:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $('#p1').ready(function(){
            $('#p1').css({"color": "green"}).css({"fontFamily": "Arial"}).css({"fontSize": "24px"});
        });
    </script>
</head>
<body>
    <p id="p1">Hello World!</p>
</body>
</html>