<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>My Playground</title>
<style>
table {
border-collapse: collapse;
}
th {
font-weight: normal;
background: #eee;
}
th,
td {
padding: 4px;
border: 1px solid;
text-align: center;
}
th:nth-child(1) {
width: 40px;
}
th:nth-child(2) {
width: 180px;
}
</style>
</head>
<body>
<table>
<thead>
<tr><th>#</th><th>名前</th></tr>
</thead>
<tbody id="names">
</tbody>
</table>
<script>
// 名前の配列を定義
const names = [
'佐藤',
'鈴木',
'高橋',
'田中',
'伊藤',
'渡辺',
'木村',
'吉田',
'山本',
'中村',
'小林',
'加藤'
];
// 配列のすべての要素に対して処理を実行
names.forEach((name, index) => {
// 新しいtr要素を作成
const tr = document.createElement('tr');
// インデックス用のtd要素を作成し、インデックスを設定
const indexTd = document.createElement('td');
indexTd.textContent = index + 1;
// 名前用のtd要素を作成し、名前を設定
const nameTd = document.createElement('td');
nameTd.textContent = name;
// インデックス用のtd要素をtr要素に追加
tr.appendChild(indexTd);
// 名前用のtd要素をtr要素に追加
tr.appendChild(nameTd);
// tr要素をテーブルのtbody要素に追加
document.getElementById('names').appendChild(tr);
});
</script>
</body>
</html>
投稿者: chosuke
javascript タブメニュー
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>My Playground</title>
<style>
.container {
margin: 16px auto;
width: 500px;
}
.tabs {
list-style: none;
padding: 0;
margin: 0;
display: flex;
}
.tabs li a {
display: inline-block;
width: 80px;
text-align: center;
padding: 8px;
color: #333;
text-decoration: none;
}
.tabs li a.active {
background: #333;
color: #fff;
}
.content.active {
background: #333;
color: #fff;
min-height: 150px;
padding: 12px;
display: block;
}
.content {
display: none;
}
</style>
</head>
<body>
<div class="container">
<ul class="tabs">
<li><a href="#" class="active" data-id="p1">商品1</a></li>
<li><a href="#" data-id="p2">商品2</a></li>
<li><a href="#" data-id="p3">商品3</a></li>
<li><a href="#" data-id="p4">商品4</a></li>
</ul>
<section class="content active" id="p1">
商品1の説明です。
</section>
<section class="content" id="p2">
商品2の説明です。
</section>
<section class="content" id="p3">
商品3の説明です。
</section>
<section class="content" id="p4">
商品4の説明です。
</section>
</div>
<script>
// タブのリンク要素を取得
const tabs = document.querySelectorAll('.tabs li a');
// コンテンツの要素を取得
const contents = document.querySelectorAll('.content');
// すべてのタブ要素に対して処理を実行
tabs.forEach(clickedItem => {
// タブがクリックされたときの処理
clickedItem.addEventListener('click', e => {
// リンクによるページ遷移を抑制
e.preventDefault();
// いったんすべてのタブを非アクティブにする
tabs.forEach(item => {
item.classList.remove('active');
});
// クリックされたタブだけアクティブにする
clickedItem.classList.add('active');
// いったんすべてのコンテンツ要素を非アクティブにする
contents.forEach(content => {
content.classList.remove('active');
});
// クリックされたタブに対応するコンテンツ要素だけアクティブにする
document.getElementById(clickedItem.dataset.id).classList.add('active');
});
});
</script>
</body>
</html>
python @staticmethod
class HtmlHelper:
@staticmethod
def to_h1(str):
return f"<h1>{str}</h1>"
@staticmethod
def to_p(str):
return f"<p>{str}</p>"
print(HtmlHelper.to_h1("Hello"))
print(HtmlHelper.to_p("Wow"))
Javascript ハートをクリック
python メソッドのオーバーライド
class Post: # 親クラス Superクラス
def init(self, text):
self._text = text
self._likes = 0
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
class SponsoredPost(Post): # 子クラス Subクラス
def init(self, text, sponsor):
# self._text = text
# self._likes = 0
super().init(text)
self._sponsor = sponsor
def show(self):
print(f"{self._text} - {self._likes} sponsored by {self._sponsor}")
posts = [
Post(“Hello”),
Post(“Hi”),
SponsoredPost(“Hey”, “dotinstall”),
]
posts[2].like()
for post in posts:
post.show()
python クラスの継承
class Post: # 親クラス Superクラス
def init(self, text):
self._text = text
self._likes = 0
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
class SponsoredPost(Post): # 子クラス Subクラス
pass
posts = [
Post(“Hello”),
Post(“Hi”),
SponsoredPost(“Hey”),
]
for post in posts:
post.show()
python メソッドの定義
class Post:
_count = 0
def init(self, text):
self._text = text
self._likes = 0
Post._count += 1
@classmethod
def show_count(cls):
print(f"{cls._count} instances created")
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
posts = [
Post(“Hello”),
Post(“Hi”),
]
for post in posts:
post.show()
Post.show_count()
python @property
class Post:
def init(self, text):
self._text = text
self._likes = 0
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
@property
def likes(self):
return self._likes
posts = [
Post(“Hello”),
Post(“Hi”),
]
posts[0].likes = 100
print(posts[0].likes)
python setter、getter
class Post:
def init(self, text):
self._text = text
self._likes = 0
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
def set_likes(self, num):
self._likes = num
def get_likes(self):
return self._likes
posts = [
Post(“Hello”),
Post(“Hi”),
]
posts[0]._likes = 100
print(posts[0]._likes)
posts[0].set_likes(100)
print(posts[0].get_likes())
for post in posts:
post.show()
python 属性へのアクセス
class Post:
def init(self, text):
self.text = text
self._likes = 0
def show(self):
print(f"{self.text} - {self._likes}")
def like(self):
self._likes += 1
posts = [
Post(“Hello”),
Post(“Hi”),
]
posts[0]._likes += 1
posts[0].like()
for post in posts:
post.show()