2016-08-02 16 views
0

私はindexofやregexのように、創造された単語のスタイルを変更する方法を知っています。インデックスまたはなどだから私はそのようsmthingやりたいまもなく場合:? 言葉を見つける: OSQ、kkw テキスト: のAAA BBB OSQOSQ qjweqj kkwテキスト内の単語を見つけて、テキスト内の単語を太字と色にする:赤色

$(".findwords").click(function() 
{ 
    var text = $("#text").val(); 
    var words = $("#words").val(); 
    var arrword = words.split(','); 
    $.ajax({ 
     beforeSend:function() 
     { 
      $("#lback-words").show(); 
      $("#lback-text").show(); 
      $('.loader').show(); 
     }, 
     url: "/Demo2/Fwords/", 
     data: data = { "text": text, "words": words }, 
     method: 'POST', 
     dataType: 'json', 
     success: function (data) { 
      $('#text').val(' '); 
      $('#words').val(' '); 
      setTimeout("$('.loader').hide()", 3000); 
      setTimeout("$('#lback-words').hide()", 3000); 
      setTimeout("$('#lback-text').hide()", 3000); 
      console.log(data); 
      var sub_ul = $('<ul/>'); 
      $.each(data, function (index, value) { 
       console.log(value); 
       var sub_li = $('<li/>'); 
       $(sub_li).html(value.w + "-" + value.q); 
       $(sub_li).appendTo(sub_ul); 
      }); 
      var appendContent = $("<section class='searchresult'><h4>Result:</h4><div class='words'>Words:</div><div class='text'>Text:<br>" + text + "</div></section>"); 
      $(appendContent).find(".words").append(sub_ul); 
      $(".texnwords").append(appendContent); 
     }, 
     error: function (data) { 
     console.log(data); 
     } 
}) 
+0

でのdivを追加しますか?いくつかのCSSでスパンの内側?強いタグ?あなたのコードを少し投稿してください。 – yuriy636

+0

thats私のappendcontent dunnoより良い方法をいくつかスパンを追加するか? var appendContent = $( "<セクションクラス= '検索結果'>

結果:

Words:
Text:
" + text + "
"); – vtarbinskyi

答えて

0

はこれを試してみてください。

var html = $('.your_text_element').html(); 
html = html.replace('osq', '<b>osq</b>'); 
$('.your_text_element').html(html); 

更新:youtはHTMLで

// get values of text and words 
var text = $("#text").val(); 
var words = $("#words").val(); 
// check if text contains words 
var text_after_check = text.replace(words, '<b>' + words + '</b>'); 
if(text != text_after_check){ 
    // words found 
    // add html with hightlighted text to div check_result 
    $('#check_result').html(text_after_check); 
    // hide textarea (optional) 
    $("#text").toggle();   
} 

あなたはその言葉を太字にするにはどうすればよいのid = check_result

+0

m8が働いていますが、もし私がそれを持っていれば、valでそれを行う方法を尋ねることができます:var text = $( "#text")。 var words = $( "#words")。val(); var arrword = words.split( '、');私はarrwordからすべての単語のためにこのことをする必要があります。 – vtarbinskyi

+0

入力フィールド内にハイライトテキストが必要であることを意味していますか? – stweb

+0

私は入力とテキストエリアを持っていることを意味し、次にボタンをクリックして、divの1つのセクションをテキストの別の単語として追加します。この1つはテキストです。太字と赤のように変更する必要があります。それらがテキスト内に存在する場合、単語。 – vtarbinskyi

関連する問題