预先设定div,但不关闭它们 [英] Prepend divs without closing them

查看:132
本文介绍了预先设定div,但不关闭它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div class =section map-wrapper

使用Jquery在div中添加以下内容: >
< div class =wrapper section group inner-map-wrapper>
< div class =col span_12_of_12>

为此,我使用以下Jquery:

  //除了第一个
$('< div class =section map-wrapper> ').prependTo($(' mpfy标签清单。));
$('< div class =wrapper section group inner-map-wrapper>')。prependTo($('。map-wrapper'));
$('< div class =col span_12_of_12>')。prependTo($('。inner-map-wrapper'));

问题在于它关闭了每个div,我不希望它这样做。我怎样才能把HTML放在顶端?然后,我会在后面添加结束div。 你不能。尽管它提供了抽象,但jQuery不支持HTML,它在DOM上运行(没有开始标签,也没有结束标签,只是元素)。



构建要预先设置的DOM,然后添加它。

  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);


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.

解决方案

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);

这篇关于预先设定div,但不关闭它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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