简体   繁体   中英

jQuery: Wrap outputted text in span?

I have a jQuery script that inserts an <input> after the outputted text. My question is how do I wrap the outputted text in a <span> before the <input> is called?

$(".todo-deadline").each(function() {
    $(this).html($(this).text() + "<input readonly='readonly' class='hasDatepicke2' type='text' value='"+$(this).text()+"' />");
});

Try

$(".todo-deadline").each(function() {
    var $this = $(this), text = $this.text();
    $this.empty().append($('<span />', {
        text: text
    })).append('<input readonly="readonly" class="hasDatepicke2" type="text" value="' + text + '" />')
});

Demo: Fiddle

Or

$(".todo-deadline").html(function(idx, html) {
    return '<span>' + html + '</span>' + '<input readonly="readonly" class="hasDatepicke2" type="text" value="' + html + '" />'
});

Demo: Fiddle

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