、私はそれに答えるためにつもりはなかったが、私は(私の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の答えです。 javacriptの答えは以下のようになります。
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)。
これは、簡単に公式jQueryのドキュメントを参照することによって研究されています。 http://api.jquery.com/append/ – Patrick
あなたはあなたが望む情報を持っている場合、受け入れるように答えを記入することを忘れないでください –