将文字添加到< textarea>通过jquery而不是HTML [英] Adding text to <textarea> via jquery than into HTML

查看:146
本文介绍了将文字添加到< textarea>通过jquery而不是HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想把这个想法归结为我正在进行的一个简单的项目。

I've been trying to get this idea I had together for a simple project i'm working on.

我想要的结果是有一个按钮单击时添加HTML5文档的准系统标签。从那里,我希望能够添加其他HTML标签,如列表,段落,标题,页面标题等,并让它们在适当的位置,所以如果我添加< p> 标记它将被添加到正文中,但是,当我添加< title> 时,它将是在我的头脑中。

The result I would like is to have a button that when clicked adds the barebones tags of a HTML5 doc. From there, I'd like to be able to add other HTML tags to it as well such as, lists, paragraphs, headers,a page title etc. and have them be in their proper place so if I add a <p> tag it'll be added in the body however, when I'm adding the <title> it will be under the head.

我遇到的问题是我已经弄清楚如何使用它的 .val()选择器,但它在技术上是文本而不是实际的HTML所以,我不知道如何添加/删除其他元素而不以某种方式将textarea中的文本切换为实际的HTML并尝试追加或者每次从那里点击一个按钮前置。

The problem I'm running into is that I've figured out how to get the "tags" into the textarea using it's .val() selector, but it's technically text NOT actual HTML so, I don't know how I could add/remove other elements without somehow switching the text in the textarea to actual HTML and try append or prepend every time a button is clicked from there.

这是我到目前为止对jQuery的所有...

here's what I have so far for the jQuery...

$(document).ready(function () {
    $("button#setup").click(function () {
    var text_area = $("#code_window")
    $(text_area).val("<!DOCTYPE html>" + "\n" + "<html>" + "\n" + "<head>" + "\n\t\n" +        "</head>" + "\n" + "<body>" + "\n\t\n" + "</body>" + "\n" + "</html>");
    });
});

以及这里的所有内容..

and here's everything together..

http://jsfiddle.net/fHg8H/

任何你可以贡献的东西都会受到赞赏。

Anything you could contribute would be appreciated.

谢谢!

推荐答案

我会使用 .text() 。试试这个:

I would use .text(). Try this:

$(document).ready(function () {
    var text_area = $("#code_window");
    $("button#setup").click(function () {
        $(text_area).text("<!DOCTYPE html>" + "\n" + "<html>" + "\n" + "<head>" + "\n\t\n" + "</head>" + "\n" + "<body>" + "\n\t\n" + "</body>" + "\n" + "</html>");
    });
    $("button#add_p").click(function () {
        var txt = $("#code_window").val();
        var new_txt = txt.replace('</body>', '<p>' + '\n' + '</p>' + '\n' + '</body>');
        $(text_area).text(new_txt);
    });
});

这篇关于将文字添加到&lt; textarea&gt;通过jquery而不是HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆