21-2.htc

来自「JAVASCRIPT完全自学手册,中源码的验证修订实例」· HTC 代码 · 共 52 行

HTC
52
字号
<html>
<head>
<PUBLIC:COMPONENT>
    <PUBLIC:ATTACH event="oncontentready" onevent="init();"/>
    <PUBLIC:ATTACH event="onchange" onevent="save();"/>
    <PUBLIC:ATTACH event="onfocus" onevent="_onfocus();"/>
    <PUBLIC:ATTACH event="onblur" onevent="_onblur();"/>
</PUBLIC:COMPONENT>
<script>
function init(){
    if(element.id){
        if(/text|password/i.test(element.type)){
            element.value = session(element.id) || "";
        }
    }
}

function save(){
    if(element.id){
        if(/text|password/i.test(element.type)){
            session_save(element.id, element.value);
        }
    }
}

function _onfocus(){
    element.select();
    element.originalBackgroundColor = element.style.backgroundColor;
    element.style.backgroundColor = "yellow";
}

function _onblur(){
    element.style.backgroundColor = element.originalBackgroundColor;
}

function session(name){
	var re;
	re = String(document.cookie).match(new RegExp(regexp_add_slash(escape(name))+"=(.+?)(?=;|$)"));
	if(re)re=unescape(re[1]);
	return(re);
}

function session_save(name, value){
	var dt = new Date();
	dt.setYear(2200);
	document.cookie = escape(name) + "=" + escape(value) + ";expires=" + dt.toUTCString() + ";";
}

function regexp_add_slash(str){ return(str.replace(/(\W)/g,"\\$1")); }
</script>
</head>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?