开发者

Jquery replace text live doesn't works

开发者 https://www.devze.com 2023-04-10 12:28 出处:网络
this is my js. $(\'.wysiwyg\').live(\'keyup\',function(){ wysiwyg_val = $(this).val(); wysiwyg_val = wysiwyg_v开发者_JAVA百科al

this is my js.

$('.wysiwyg').live('keyup',function(){

     wysiwyg_val = $(this).val();

    wysiwyg_val = wysiwyg_v开发者_JAVA百科al
    .replace(/\n/g, "<br />")
    .replace(/\n\n+/g, '<br /><br />')
    .replace("{code}","<pre><code>")
    .replace("{/code}","</code></pre>")
    .replace("{img}",'<img src="http://localhost/CI_DEVBASE/img/logo.png" width="150" height="50"')
    .replace("{/img}",'/>');

    $('.wysiwyg-preview').html(wysiwyg_val);


  });

the html is.

<textarea class="wysiwyg"></textarea>
<div class="wysiwyg-preview"></div>

this code works if i put just 1 {code}{/code} inside the textarea, but if i put 2 {code}{/code} {code}{/code} it replaces only the first one, how can i apply this function to all the text inside wysiwyg_val ?


g should be used for global replacement. Also I have used \ to escape special characters like {,/ etc. Here's the corrected code for you:

wysiwyg_val = wysiwyg_val
    .replace(/\n/g, "<br />")
    .replace(/\n\n+/g, '<br /><br />')
    .replace(/\{code\}/g,"<pre><code>")
    .replace(/\{\/code\}/g,"</code></pre>")
    .replace(/\{img\}/g,'<img src="http://localhost/CI_DEVBASE/img/logo.png" width="150" height="50"')
    .replace(/\{\/img\}/g,'/>');


Hej Ispuk

you already have the solution in your code

.replace(/\n/g, "<br />")

the g tels it to do it globally. Use the same technique with the code blocks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号