-3

テーブル内に動的にタグを作成する方法。最初にリンクを作成し、次にリンク内にimgタグを作成します。

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

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

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

この内部関数を使用していますが、機能しませんでした。

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

ボタンをクリックするたびに、画像URL abc.pngのimgタグが追加され、idimagedivを持つ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 に答える
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を利用して、以下のように画像要素をcnacrateします。

$(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 に答える