TypeScript

by Sokiraon

HTML

<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<div>
	<button type="button" onclick="stot()">
		簡轉繁
	</button>
	<button type="button" onclick="ttos()">
		繁转简
	</button>
</div>
<h2 id="target">
	要转换的文字
</h2>

JavaScript

const target = document.getElementById("target");

function stot() {
	OpenCC.Converter('cn', 'tw')
		.then(convert => {
			target.innerText = convert(target.innerText);
		});
}

function ttos() {
	OpenCC.Converter('tw', 'cn')
		.then(convert => {
			target.innerText = convert(target.innerText);
		});
}