Last active
March 13, 2018 08:32
-
-
Save sounisi5011/7dffbc2a33a20d52fafb55c0e10d969a to your computer and use it in GitHub Desktop.
入力した文字を括弧で囲むだけのクソアプリ(Vanilla JS版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset=utf-8> | |
<meta name=viewport content="width=device-width,initial-scale=1"> | |
<meta name=format-detection content="telephone=no,email=no,address=no"> | |
<title>入力した文字を括弧で囲むだけのクソアプリ - Vanilla JS版</title> | |
<link rel=stylesheet href="main.css"> | |
<h1>入力した文字を括弧で囲むだけのクソアプリ - Vanilla JS版</h1> | |
<main id=main></main> | |
<script src="main.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
text-align: center; | |
} | |
#main input { | |
font-size: 1em; | |
} | |
#main input[type=button] { | |
margin-left: .5em; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列を括弧で囲む関数 | |
* 空文字列の場合は、空文字列を返す | |
* @param {string} text 括弧で囲む文字列 | |
* @return {string} 括弧で囲まれた文字列。または、空文字列 | |
*/ | |
function wrapBracket(text) { | |
var newText = ''; | |
if (text !== '') { | |
newText = '「' + text + '」'; | |
} | |
return newText; | |
} | |
/* | |
* 要素を生成 | |
*/ | |
var inputElem = document.createElement('input'); | |
var buttonElem = document.createElement('input'); | |
var outputElem = document.createElement('p'); | |
/* | |
* 入力欄の属性を設定 | |
*/ | |
inputElem.type = 'text'; | |
inputElem.placeholder = '文字を打って!!!'; | |
/* | |
* 表示ボタンの属性とイベントを設定 | |
*/ | |
buttonElem.type = 'button'; | |
buttonElem.value = '括弧で囲む'; | |
buttonElem.addEventListener('click', function() { | |
/* | |
* 入力値を取得 | |
*/ | |
var inputText = inputElem.value; | |
/* | |
* 入力値を括弧で囲む | |
*/ | |
var outputText = wrapBracket(inputText); | |
/* | |
* 表示 | |
*/ | |
outputElem.textContent = outputText; | |
}, false); | |
/* | |
* 要素をDOMに追加 | |
*/ | |
var mainElem = document.getElementById('main'); | |
mainElem.appendChild(inputElem); | |
mainElem.appendChild(buttonElem); | |
mainElem.appendChild(outputElem); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment