2012-03-02 12 views
1

私は自分のウェブページでさまざまな色を変える方法を考えています。 EG:「これ」は赤色に変わりますが、「ボックス」は青色に変わります。特定の単語を異なる色にするにはどうすればよいですか?

は、これまでのところ私はこれを持っている:

<div class="scroll"> 
      <p class="margin">This box should scroll when it runs out of space.This box should     scroll when it  runs out of space.This box should scroll when it runs     out of space.This box should scroll when it runs  out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out  of space.This box should scroll when it runs     out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space.This box should scroll when it runs out of   space.This box     should scroll when it runs out of space.This box should scroll when it runs out     of  space.This box should scroll when it runs out of space.This box     should scroll when it runs out of  space.This box should scroll     when it runs out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space.This box should scroll when it runs out of   space.This box     should scroll when it runs out of space.This box should scroll when it runs out     of  space.This box should scroll when it runs out of space.This box     should scroll when it runs out of  space.This box should scroll     when it runs out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space.This box should scroll when it runs out of   space.This box     should scroll when it runs out of space.This box should scroll when it runs out     of  space.This box should scroll when it runs out of space.This box     should scroll when it runs out of  space.This box should scroll     when it runs out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space.This box should scroll when it runs out of   space.This box     should scroll when it runs out of space.This box should scroll when it runs out     of  space.This box should scroll when it runs out of space.This box     should scroll when it runs out of  space.This box should scroll     when it runs out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space.This box should scroll when it runs out of   space.This box     should scroll when it runs out of space.This box should scroll when it runs out     of  space.This box should scroll when it runs out of space.This box     should scroll when it runs out of  space.This box should scroll     when it runs out of space.This box should scroll when it runs out of     space.This box should scroll when it runs out of space.This box should scroll     when it runs out of   space.This box should scroll when it runs out     of space. 
     </p> 
    </div> 

編集:

うーん、申し訳ありません。私はあなたが何を言っているのか、それをどう使うべきかを本当に理解していません。

+0

色をランダムである?または、各単語に独自の色を付けることを望みますか? –

+0

私はそれが独自の色を持っています – EandCProducts

答えて

1

色を変更したい単語の周りにスパンを作成することができます。

私ははい、私は質問がどのjavascriptまたはjqueryタグをしていない知っているが、これは:)

が@Nic Meiringの答えにこれを追加提案する可能性がありhttp://jsfiddle.net/LwR8D/

+0

CSSでは 'class'を使用し、この場合は' id'ではない方が良いと思います(ページ上の何百もの "This") –

+0

が合意しました。あなたが望むなら、たとえば、このページの "This"のすべてのインスタンスが同じ色になるようにするとよいでしょう。 –

+0

ありがとう。これは私を非常に助けてくれました。 – EandCProducts

-1

であなたのためのデモを作成しました:

ページのご<header></header>セクションに次の行を追加します。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 

は、その後、あなたのページに次のJavaScriptを呼び出すための別の<script></script>セクションを追加 <header></header>セクション:

(function($) { 
    var thePage = $("body"); 
    thePage.html(thePage.html().replace(/This/ig,'<span id="This">This</span>')); 
})(jQuery) 

Fiddle link

Use jQuery to Replace a Word

+0

@nicの最新バージョン:http://jsfiddle.net/LwR8D/3/ – Jalal

+0

が 'class' instate of' id'に更新されました。http://jsfiddle.net/ LwR8D/4/ – Jalal

+0

ええと、私はこのようなコードは見たことがありません、どのようなコードですか? – EandCProducts

2

Nic Meiringは、有効な解決策に答えが、して、長い段落で色を変更することは困難です特定の単語を見つけてそれらにCSSを適用する。短いテキストの場合は問題ありませんが、長い段落や可変テキストの場合は、一般的な解決策を適用する必要があります。

CSSソリューション:(すべてのブラウザわからない)あなたが使用することを許可されている場合は

jQuery

またはサーバー側言語で文字列置換方法を使用できます。 innerHTMLプロパティと

$text = str_replace("red", "<span class='red-class'>red<span>", "This is red"); 
+0

は、これがより持続可能な解決策であることを@NAVEEDと100%合意しました。 HTML、CSS、スタイリングタグの中にとどまるだけでした:) –

0

ダーティ-しかし、作業溶液:それで

var words_and_colors = [ 
    {'word':'this', 'color':'red'}, 
    {'word':'to', 'color':'blue'} 
]; 

function hilte_words_in_element (element, words) { 
    for (var z=0; z < words.length; z++) { 
     var re = new RegExp('('+words[z]['word']+'\\b)', 'gi') 
     element.innerHTML = element.innerHTML.replace(
      re, 
      "<span style=\"color:"+words[z]['color']+"\">$1</span>" 
     ); 
    } 
} 
hilte_words_in_element(document.getElementById('changeMe'), words_and_colors); 

フィドル:PHPでの例str_replaceについては、これは "悪い" と考えていることをhttp://jsfiddle.net/8vDgH/

注 - 1がすべきそれが避けられればinnerHTMLを扱わないでください。あなたがそれを使用している状況を知らずに、私はDOMとのより具体的な対話を行うことができません。あなたが使っているものを知っているので、コードをリファクタリングして、ノードの内容を変更するより受け入れやすい手段を使用することをお勧めします。

これがサーバー側で実行できる場合は、はるかに効率的です。同様のコンセプトを使用しますが、コンテンツが出力される前に行います。これがあなたのユースケースで実行可能かどうかは分かりませんが、もしそうなら、最高のパフォーマンスを持つルートとなることは間違いありません(innerHTMLを避けることができます)

+0

どのようにして各単語を別の色にするのですか? "This"だけではありません – EandCProducts

+0

上の配列には、色を変更する単語のリストと対応する色が格納されています。この例では、単語「to」と「this」が変更されています。 –

関連する問題