开发者

Live preview and HTML

开发者 https://www.devze.com 2023-01-20 16:43 出处:网络
Well I have the li开发者_开发知识库ve preview of text going using jQuery. http://jsbin.com/ezuta4

Well I have the li开发者_开发知识库ve preview of text going using jQuery.

http://jsbin.com/ezuta4

But is there a way where I can put in HTML tags and the HTML won't show but effects the text? Like typing <h1> and the tags turn into headings?

So far:

   $(document).ready(function(){

  $('#text').keypress(function() {
  $('#live').text($(this).val());
  });
}); // end jQuery 
​


text() will parse anything you enter as literal characters. To allow HTML code, use

  $('#live').html($(this).val());

http://jsbin.com/ezuta4/2


Use .html() instead of .text(). This won't escape what you type. Also use .keyup() instead of .keypress(), or else the last character you press won't show until you press something else, the event will happen before the character is rendered.

$('#text').keyup(function() {
    $('#live').html($(this).val());
});

jQuery reference

  • .html
  • .text
  • keyup
  • keypress


use .html() instead of .text()

0

精彩评论

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