dak ブログ

python、rubyなどのプログラミング、MySQL、サーバーの設定などの備忘録。レゴの写真も。

実行中の script タグの要素を取得する方法

2022-01-01 23:23:58 | javascript
実行中の script タグの要素を取得する方法のメモ。

実行中の script タグの要素は document.currentScript で取得できます。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>script</title>
</head>
<body>
<div>123</div>
<script type="text/javascript">
var e = document.currentScript;
var c = document.createElement('div')
c.innerText = 'def';
e.parentNode.append(c);
</script>
456
</body> </html>