Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

jquery - Prepend divs without closing them

I want to prepend the following within a div using Jquery:

<div class="section map-wrapper">
    <div class="wrapper section group inner-map-wrapper">
        <div class="col span_12_of_12">

To do this I'm using the following Jquery:

// prepend a 'previous' button to all form-rows except the first    
$('<div class="section map-wrapper">').prependTo($('.mpfy-tags-list'));
$('<div class="wrapper section group inner-map-wrapper">').prependTo($('.map-wrapper'));
$('<div class="col span_12_of_12">').prependTo($('.inner-map-wrapper'));

The problem with this is that it's closing each div, which I don't want it to do. How can I prepend the html at the top exactly as it is? I'll then append the closing divs later on.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't.

Despite the abstraction it offers, jQuery doesn't operate on HTML, it operates on a DOM (where there are no start tags and no end tags, just elements).

Build the DOM you want to prepend, and then prepend it.

var $wrapper = $('<div class="section map-wrapper">');
var $inner = $('<div class="wrapper section group inner-map-wrapper">')
var $col = $('<div class="col span_12_of_12">');
$inner.prepend($col);
$wrapper.prepend($inner);
$('.mpfy-tags-list').prepend($wrapper);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...