-3

How to create dynamically tag inside table. At first create link then inside link create an img tag like if i have..

<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 using this inside function but its didn't work help me.

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

6 回答 6

2

如果您通过“jquree”表示 JQuery,那么试试这个:

$('table tr td').append('<a href="#"><img src="/favicon.ico"/></a>');
于 2012-04-04T07:16:43.033 回答
2

好吧,我不打算回答这个问题,但我没有看到任何正确的答案(来自我的 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"
}

现在这是一个纯粹的 jQuery 答案。一个 javascript 答案如下:

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

我认为js版本更具可读性。但这只是我的意见;o)。

于 2012-04-04T07:29:46.333 回答
0
$(document).ready(function(){

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

});
于 2012-04-04T07:13:56.643 回答
0

使用 jquery,然后你可以创建图像元素,如下所示

$(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
}

或者

var img = new Image(1,1);  ///params are optional 
img.src = ''pic.jpg'; 
于 2012-04-04T07:15:10.493 回答
0
var td = $('table tr td');
td.append('<a><img src="whatever.jpg"/></a>');
于 2012-04-04T07:15:15.860 回答
0

在每次单击按钮时,它将添加带有图像 url abc.png 的 img 标签,并添加到具有 id imagediv 的 div 中。

$("button").click(function()
 {
     var img=$('<img id="dynamic">');     
     $(document.createElement('img'));
     img.attr('src',"abc.png");
     img.appendTo('#imagediv');
  });
于 2012-04-04T08:01:47.817 回答