Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQueryでインデックスを指定して要素を追加する
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>デモ</title>
</head>
<body>
<ul id="list">
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
<script type="text/javascript" src="assets/lib/jquery-2.0.2.min.js"></script>
<script type="text/javascript">
var newListItem = $('<li>').text('add text');
// 以下の2通りで追加可
newListItem.insertBefore($('#list').children()[1]);
$($('#list').children()[1]).before(newListItem);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.