2012-04-04 4 views
-3

So erstellen Sie dynamisch Tag innerhalb der Tabelle. Auf den ersten Link in Verbindung erstellen dann ein img-Tag wie erstellen, wenn ich ..dynamisch img-Tag mit jQuery erstellt?

<table> 
    <tr> 
     <td> 
      <a> 
       <img /> 
      </a> 

      // Add Some more when every time my function is run..? like that 
      // <a> 
      // <img/> 
      // </a> 

     </td> 
    </tr> 
</table> 

Im diese innerhalb Funktion aber seine funktionierte mir nicht helfen.

$(document.createElement("img")).attr('Some attr'); 
+0

Dies wird einfach durch Beratung die offizielle Dokumentation jQuery recherchiert. http://api.jquery.com/append/ – Patrick

+1

Vergessen Sie nicht, die Antwort als akzeptiert zu markieren, wenn Sie die Informationen erhalten, die Sie wollen –

Antwort

2

Wenn Sie JQuery bedeuten von "jquree" Versuchen Sie dann Folgendes:

$('table tr td').append('<a href="#"><img src="/favicon.ico"/></a>'); 
0
$(document).ready(function(){ 

    $('.any_element_you_want').html('<a href="/home" title="Home"><img src="image.png"></a>'); 

}); 
0

Nutzen Sie jquery und als Sie CNA Kiste Bildelement wie wie unten

$(document).ready(function(){ 

    var elem = new Element('img', 
       { src: 'pic.jpg', alt: 'alternate text' }); 
    $(document).insert(elem); //here you can also make use of `append` method instead of this method 
} 

oder

var img = new Image(1,1); ///params are optional 
img.src = ''pic.jpg'; 
+0

Ich versuche dies, ich denke, es funktioniert. – QasimRamzan

+0

@QasimRamzan - dann akzeptieren und Upvote Antwort, wenn es Ihre Anforderung erfüllen –

0
var td = $('table tr td'); 
td.append('<a><img src="whatever.jpg"/></a>'); 
2

Nun, ich würde antworten, dass es nicht, aber ich bin keine richtige Antwort zu sehen (von meiner POV):

function addElement(tdId) { // Specify the id the of the TD as an argument 
    $('#' + tdId).append(// Append to the td you want 
     $('<a></a>').attr({ // Create an element and specify its attributes 
      'href': '/home', 
      'title': 'Home' 

     }).append(// Also append the image to the link 
      $('<img />').attr({ // Same, create the element and specify its attributes 
       'src': 'image.png', 
       'width': '100px', 
       'height': '100px' 
      }) 
     ) // Close the "append image" 
    ) // Close the "append anchor" 
} 

Nun, da eine reine Antwort jQuery ist. Eine JavaScript-Antwort wäre folgende:

function addElement(tdId) { // Specify the id the of the TD as an argument 
    // Create the DOM elements 
    var a = document.createDocumentFragment('a'), 
     img = document.createDocumentFragment('img') // See the use of document fragments for performance 

    // Define the attributes of the anchor element 
    a.href = '/home' 
    a.title = 'Home' 

    // Define the attributes of the img element 
    img.src = 'image.png' 
    img.width = '100px' 
    img.height = '100px' 

    // Append the image to the anchor and the anchor to the td 
    document.getElementById(tdId).appendChild(a.appendChild(img)) 
} 

Ich denke, die Js-Version ist lesbarer. Aber das ist nur meine Meinung; o).

0

Auf jeden Klick der Schaltfläche, wird img Tag mit Bild URL abc.png hinzufügen und zu Div mit ID imagediv hinzufügen.

$("button").click(function() 
{ 
    var img=$('<img id="dynamic">');  
    $(document.createElement('img')); 
    img.attr('src',"abc.png"); 
    img.appendTo('#imagediv'); 
    });