简体   繁体   English

jQuery将HTML代码添加到页面

[英]jquery add HTML code to the page

I have a this HTML Code in a separate file and I have to add it to a another HTML page. 我在单独的文件中有此HTML代码,并且必须将其添加到另一个HTML页面中。

<!-- TAGLIO DONNA MEDIO -->                                               
<div class="form-inline divNoMargin col-md-12 def_taglioDonnaMedio noPadding ">
    <!-- TITLE -->
    <label class="col-md-5 noPaddingLeft divNoMargin defTaglioDonnaMedioTitle testoPiccolo">Taglio Donna Medio</label>

    <!--OPRZIONI PREZZO -->                                    
    <!-- SELECT 1 -->
    <div class=" col-md-8 divNoMargin  select1">
        <label class="testoPiccolo pull-left">prezzo:</label>
        <label for="" id="" class="col-md-4 pull-right testoMedio text-right prezzo defPrezzoTagliodonnaMedio">,00</label>
    </div>        

    <!-- SELECT 2 -->
    <div class=" col-md-8 noMarginLeft2 hidden select2">
        <label class="pull-left testoPiccolo">prezzo a partire da:</label>
        <label for="" id="" class="pull-right col-md-4 testoMedio text-right prezzo noMarginLeft2 defPrezzoTagliodonnaMedio">,00</label>
    </div>         

</div>

I tried using this jquery but doesn't work. 我尝试使用此jQuery,但不起作用。 Anyone 任何人

var parentDiv2 = $(document).find('.nuoviServiziReview1');
$.get( "nuoviServiziReview.html", function( data ) {
    var nuovoServizioReview = $('<div/>',{id:'Servizio'+ incremento}).append(parentDiv2);
    nuovoServizioReview.html(data);
})

It is because, you are not doing anything to the DOM: 这是因为,您没有对DOM做任何事情:

var parentDiv2 = $(document).find('.nuoviServiziReview1');
$.get( "nuoviServiziReview.html", function( data ) {
    var nuovoServizioReview = $('<div/>',{id:'Servizio'+ incremento});
    nuovoServizioReview.html(data);
    nuovoServizioReview.appendTo(parentDiv2);
})

Have you tried load? 您是否尝试加载?

http://api.jquery.com/load/ http://api.jquery.com/load/

Description: Load data from the server and place the returned HTML into the matched element. 描述:从服务器加载数据并将返回的HTML放入匹配的元素。

$('.nuoviServiziReview1').load('nuoviServiziReview.html');

append(content) appends content to the inside of every matched element. append(content)将内容追加到每个匹配元素的内部。 appendTo(selector) appends all of the matched elements to another specified set of elements. appendTo(selector)将所有匹配的元素追加到另一个指定的元素集。

Also

A.append(B) appends B to A
A.appendTo(B) appends A to B

So you need to use appendTo() instead of append() as: 因此,您需要使用appendTo()而不是append()作为:

var parentDiv2 = $(document).find('.nuoviServiziReview1');
$.get( "nuoviServiziReview.html", function( data ) {
    var nuovoServizioReview = $('<div/>',{id:'Servizio'+ incremento});
    nuovoServizioReview.html(data);
    nuovoServizioReview.appendTo(parentDiv2);
});

This should work :) 这应该工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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