How to use jQuery html() function?
When I do this:
$('.myDiv').html("<strong>Hello World</strong>");
The output inside myDiv is literally:
<strong>Hello World</strong>
instead of Hello World simply being bolded. Is there any way to use html inside the html() function or a similar function that actually translates html instead of showing it literally?
EDIT
oops I just realized the above code actually does work. My problem was I am trying to replace the contents of a textarea with html inside and that echoed out the html because that's w开发者_开发知识库hat happens when you put html inside a textarea anyway. So nothing to do with jQuery.
Are you sure you're not using .text()
?
$('.myDiv').html("<strong>Hello World</strong>");
works for me.
See this jsFiddle.
It will display Hello World in bold when the browser actually renders the HTML. You must be using Firebug or a similar tool to inspect the contents of the div
you modified. Tools like that don't render the content you are trying to inspect the way the browser would; that's the whole reason you are inspecting the markup. If they did render what you were trying to examine, well, that would just be cruel.
精彩评论