简体   繁体   中英

javascript insert text in specific place in text area

I write a form that inserts some xml tags into textarea. I use this function:

    (function ($) {
     addCustomTag = function (name, param, value) {
         var code = "<" + name + " " + param + "=\"" + value + "\">\n</" + name + ">";
         document.getElementById("codeArea").value += code;
     };
 })(jQuery);

How can I make that some other function will insert subtags into tags that were created before? XML code will never be used on server. All I need is to insert tex in specific line which is depends on what was on this line before not cutting it. Something like this:

    addCustomSubtag = function(name,param,value,parent) {
    document.getElementById("codeArea").selectionStart = document.getElementById("codeArea").value - parent.length;
    var code = "<" + name + " " + param + "=\"" + value + "\">\n</" + name + ">";
    document.getElementById("codeArea").value += code;
};

Javascript isn't necessary. It also can be written on jQuery. Thanks.

这可能会很有用: http : //dwieeb.github.com/jquery-textrange/

You can any of these jQuery functions

Update:

Actually we can use jQuery DOM manipulation methods to manipulate XML also.

var xml = "<main/>";
alert(xml);                                  // <main/>
var $xml = $(xml).append($("<sub1/>"));
alert($xml.html());                          // <sub1></sub1>
$xml.find("sub1").append($("<sub2/>"));
alert($xml.html());                          // <sub1><sub2></sub2></sub1>
alert($xml.get(0).outerHTML);                // <main><sub1><sub2></sub2></sub1></main>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM