2016-03-30 18 views
0

JQuery'de bir elemanın içinde bir eleman oluşturmaya çalışıyorum, ancak denediğimde ve çıktı aldığımda hiçbir şey elde edemiyorum. Jquery ile bir öğede öğe oluşturma

Bu

HTML eşdeğerdir ben aradığım:

HTML:

<div class="btn-icn"> 
    <button class="btn"> 
     <span class="glyphicon glyphicon-comment"></span> 
    </button> 
</div> 

jQuery:

a = $(button).attr({ 
    class: "btn"; 
    id:"btn"; 
}.append($(icon).attr({ 
    class:"glyphicon glyphicon-comment"; 
    id="comment"; 
})); 

alert(a); 
+0

iç 'button' ve' icon' değişkenler nelerdir? – llamerr

+1

Bu girinti olsa ... Oldukça emin bir yerde bir sözdizimi hatası var. –

+0

@ Karl-AndréGagnon bir değil ama 5 sözdizimi hatası – soyuka

cevap

1

$("<button/>", { // here goes the properties: 
 
    appendTo : ".btn-icn", 
 
    class : "btn", 
 
    id : "btn", 
 
    on : { 
 
     click : function(){ alert(this.tagName); } 
 
    }, 
 
    append : $("<span/>", { 
 
    class : "glyphicon glyphicon-comment", 
 
    id : "comment" 
 
    }) 
 
});
@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    
 
<div class="btn-icn"></div>

(Tek bir sayfada birden çok kez kimlik kullanmanın yanlış olduğunu unutmayın. yerine sınıflar ile sopa)

0

jquery kodu tamamen bana bozuldu.

var a = $('<a />').attr({ 
    class: "btn", //comma not semicol 
    id:"btn" 
}); 

var icon = $('<i></i>').attr({ 
    class: "glyphicon glyphicon-comment", 
    id: "comment" //no equal sign in an object, property: value 
}); 

a.append(icon) 
İlgili konular