开发者

Using the jquery html() function

I have a paragraph

开发者_StackOverflow社区
<p>hello<br>wor&nbsp;l&nbsp;d</p>

then I want show the paragraph's html in another textarea use jquery html() method

    $(function () {
        var temp = $('p').html();

        $("textarea").html(temp);
    });

but the result is

hello<br>wor l d

Why does <br> not work? I didn't change any html.

Here is the example.


? isn´t that clear? you are requesting the html and this one is pasted into the textbox? You will have to replace the br into linebrakes, like "\n"

var temp = $('p').html();

 $("#tt").html(temp.replace(/<br\s*(\/|)>/gi, '\n'));


A textarea simply displays text, not formatted HTML.


Yes, if you want to output it as displayed, the following code should help:

$(function() {
    var temp = $('p').html();
    var textareaval = temp.replace("<br>", "\n");
    textareaval = textareaval.replace("<br />", "\n");
    $('textarea').html(textareaval);
});

See the jsFiddle here: http://jsfiddle.net/6adgA/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜