如題,
想用jquery append加入以下html
<div class="class" onClick="show(0)"><div class="class"></div><p></p></div>
目前寫法是
$('#id').append(`<div class="class" onClick=`+show(i)+`><div class="class"></div><p></p></div>`);
但會發生錯誤,懇請各位大大解答,謝謝
這寫法有嚴重的問題啊~
onClick=`+show(i)+` << 應該是onClick="+show(` + i + `)">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Loading</title>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
</head>
<body>
<div id="id"></div>
</body>
<script>
$('#id').append('<div class="class" onClick="show(' + 1 + ')"><div class="class">1</div><p>1</p></div>');
function show(i)
{
console.log(i);
}
</script>
</html>
你要的是這樣?
$('#id').append(`<div class="class" onClick=show(${i})><div class="class"></div><p></p></div>`);