HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>sum price</title>
</head>
<body>
<div class="content">
<p><b>日付:<span id="target_month"></span></b></p>
<p>
<a id="get_before_month" href="javascript:void(0);"><<前月</a>
<input type="text" id="product_name" placeholder="商品">
<input type="text" id="product_price" placeholder="価格">
<input type="button" id="add" value="追加">
<input type="button" id="clear" value="クリア">
<a id="get_next_month" href="javascript:void(0);">翌月>></a>
</p>
<table>
<thead>
<tr>
<th width="25%">日付</th>
<th width="40%">商品</th>
<th width="25%">価格</th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div>
<b><p class="sum_price">合計:0円</p></b>
</div>
</div>
</body>
</html>
body {
background-color: #a3d5d3;
width: 600px;
font-family: 'Helvetica', 'Arial', sans-serif;
margin: 0 auto;
padding: 20px;
}
div.content {
/* 要素をはみ出さない */
width: 600px;
table-layout: fixed;
word-wrap: break-word;
word-break: break-all;
overflow: visible;
}
table, input {
margin: 5px;
}
input[type=text] {
width: 120px;
}
table {
width: 550px;
}
table,tr,th,td {
font-size: 14px;
list-style-type: none;
border-radius: 3px;
border: 1px solid #000;
padding: 5px;
}
td {
background: #eeeff0;
padding-left: 12px;
}
$(function(){
// 表示対象の年月を取得
var now_date = new Date();
var now_month = now_date.getFullYear()+ '/' +(now_date.getMonth()+1);
$("#target_month").append(now_month);
// ローカルストレージから表を作成
bulidTable();
// 画面表示時に価格の合計値を計算
sum();
// 挿入した行のボタンイベントをイベントハンドラへ登録する
createDeleteEvent();
/********************
共通関数
********************/
function removeLocalStorage(name){
if(isBlank(name)) {
alert("error!!");
return false;
}
// ローカルストレージから削除
localStorage.removeItem(name);
}
function removeLocalStorageAll(){
// ★ローカルストレージをすべてクリア
localStorage.clear();
}
function getLocalstorageItem(name){
if(isBlank(name)) retun;
return localStorage.getItem(name);
}
function saveLocalstorage(name, data){
if(isBlank(name) || isBlank(data)) {
alert("error!!");
return false;
}
// ローカルストレージに新規保存or上書き
localStorage.setItem(name, data);
return true;
}
//ローカルストレージ名生成
function getLocalStorageName(ym = '') {
var base_name = '_kakeibo';
if(isBlank(ym)) {
var target_month = $("#target_month").text();
ym = target_month.replace( /\//g , "" );
}
return ym + base_name;
}
// テーブルを自動生成する
function bulidTable(){
var tableBody = "";
// テーブルを初期化
$("table tbody tr").remove();
// ローカルストレージ名取得
var localstorage_name = getLocalStorageName();
var localSt = getLocalstorageItem(localstorage_name);
// ローカルストレージのデータ取得
// JSON形式から連想配列に変換
var localStJSON = JSON.parse(localSt);
$(localStJSON).map(function(index, line){
tableBody += "<tr>";
tableBody += "<td>" + line["date"] + "</td>";
tableBody += "<td class='name'>" + line["name"] + "</td>";
tableBody += "<td class='price'>" + line["price"] + "</td>";
tableBody += '<td><input type="button" class="delete" value="削除"></td>';
tableBody += "</tr>";
});
// テーブルを生成
$('table tbody').append(tableBody);
}
// 空欄チェック
function isBlank(data){
if (data.length ==0 || data == ''){
return true;
} else {
return false;
}
}
// 合計値を求める
function sum(){
// 表の金額を取得する(tdの奇数列を取得)
var pricelist = $("table td[class=price]").map(function(index, val){
var price = parseInt($(val).text());
if(price >= 0) {
return price;
} else {
return null;
}
});
// 価格の合計を求める
var total = 0;
pricelist.each(function(index, val){
total = total + val;
});
$(".sum_price").text("合計:"+total+"円");
}
// テーブル情報を読み込みJSON形式変換して返す
function getJsonFromTable() {
var counter = 0;
var line = [];
$("table tbody tr").map(function(index, val){
line[counter] = {"date":$(val).children().eq(0).text()
, "name":$(val).children().eq(1).text()
, "price":$(val).children().eq(2).text()};
counter += 1;
});
return line;
}
function createDeleteEvent() {
$(document).on("click", ".delete", function(event) {
var target = $(event.target);
target.parents("tr").remove();
// 合計値を再計算
sum();
var line = getJsonFromTable();
// 連想配列からJSON形式に変換
var mainJSON = JSON.stringify(line);
// ローカルストレージに保存
saveLocalstorage(getLocalStorageName(), mainJSON);
});
}
/********************
各種イベント処理
********************/
// 前月リンク押下時の処理
$("#get_before_month").click(function(){
var target_month_str = $("#target_month").text();
var target_month_array = target_month_str.split("/");
var last_date = new Date(target_month_array[0], target_month_array[1]-2, 1);
var last_month = last_date.getFullYear()+ '/' +(last_date.getMonth()+1);
var last_ym = last_date.getFullYear().toString() + (last_date.getMonth()+1).toString();
$("#target_month").text(last_month);
// テーブルフォームの再作成
bulidTable();
// 合計金額の再計算
sum();
// 挿入した行のボタンイベントをイベントハンドラへ登録する
createDeleteEvent();
});
// 翌月リンク押下時の処理
$("#get_next_month").click(function(){
var target_month_str = $("#target_month").text();
var target_month_array = target_month_str.split("/");
var next_date = new Date(target_month_array[0], target_month_array[1], 1);
var next_month = next_date.getFullYear()+ '/' +(next_date.getMonth()+1);
var next_ym = next_date.getFullYear().toString() + (next_date.getMonth()+1).toString();
$("#target_month").text(next_month);
// テーブルフォームの再作成
bulidTable();
// 合計金額の再計算
sum();
createDeleteEvent();
});
// addボタン押下時の処理
$("#add").click(function(){
var name = $("#product_name").val();
var price = $("#product_price").val();
var date = new Date();
var str_date = date.getFullYear()+ '/' +(date.getMonth()+1)+ '/' +date.getDate();
// 空欄チェック
if(isBlank(name) || isBlank(price)) {
alert('空欄の項目があります。');
return;
}
// 数値チェック
if (!$.isNumeric(price)) {
alert('価格は数値で入力してください。');
return;
}
// 行を追加
$('table').append('<tr><td>'+ str_date +'</td>'
+'<td class="name">'+ name +'</td>'
+'<td class="price">'+ price +'</td>'
+'<td><input type="button" class="delete" value="削除"></td>'
+'</tr>');
//ローカルストレージに保存
var product = {"date":str_date, "name":name, "price":price};
//既存のローカルストレージの値を取得
var mainArray = [];
var localStJSON = getLocalstorageItem(getLocalStorageName());
if(localStJSON != null && localStJSON != "") {
// JSON形式から連想配列に変換
var mainArray = JSON.parse(localStJSON);
}
mainArray.push(product);
// 連想配列からJSON形式に変換
var mainJSON = JSON.stringify(mainArray);
// ローカルストレージに保存
saveLocalstorage(getLocalStorageName(), mainJSON);
// 合計値を再計算
sum();
// 挿入した行のボタンイベントをイベントハンドラへ登録する
createDeleteEvent();
});
// clearボタン押下時の処理
$("#clear").click(function(){
if(!confirm('当月分のデータを削除します。よろしいですか?')){
return false;
}else{
removeLocalStorage(getLocalStorageName());
$("table tbody tr").remove();
sum();
}
});
});
Also see: Tab Triggers