Javascript 中对HTML编码和解码的方法
String.prototype.HTMLEncode = function() {
var temp = document.createElement ("div");
(temp.textContent != null) ? (temp.textContent = this) : (temp.innerText = this);
var output = temp.innerHTML;
temp = null;
return output;
}
String.prototype.HTMLDecode = function() {
var temp = document.createElement("div");
temp.innerHTML = this;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
}
上面代码转自:http://www.jb51.net/article/18396.htm
本人对javascript的核心技术不是太熟悉,再加上现在又在用不熟悉的EXT来编写前台页面代码,所以只得用最笨的方法,不扩展String自己写处理函数:
htmlDecode:function(str){
var temp = document.createElement ("div");
temp.innerHTML = str;
var output = temp.innerText || temp.textContent;
temp=null;
return output;
}
htmlEncode:function(str){
var temp = document.createElement ("div");
(temp.textContent != null) ? (temp.textContent = str) : (temp.innerText = str);
var output = temp.innerHTML;
temp = null;
return output;
}
其中用到的textContent和innerText 属性取到的内容是一样的
原因是firefox不支持innerText属性,但其提供的textContent属性和innerText具有一样的作用。所以上面的代码可以兼容IE和firefox
相关文档:
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetL ......
window.οnbefοreunlοad=function()
{
alert("onbeforeunload event...");
//return false;
}
测试2
JavaScript基础
stringObject.charAt(index)方法:返回指定索引位置处的字符。
stringObject.slice(start,[end])和stringObject.substring(start,[end])方法都接受两个参数,分别为子字符串的起始位置和终止位置,返回这两者之间的字符串,不包括终止位置的那个字符串。如果不指定第二个参数,则默认为字符串的长度,即 ......
JavaScript动态加载CSS的三种方法 收藏
如果你有很多关联的CSS文件要一起加载,或者想动态的加载不同的CSS文件,那么下面的方法你一定对你有帮助。
第一种:一般用在外部CSS文件中加载必须的文件
程序代码
@import url(style.css);
/*只能用在CSS文件中或者style标签中*/
第二种:简单的在页面中加载一 ......
添加
var oDiv = document.createElement("DIV");
oDiv.id = "shop01";
oDiv.style.top = 200;
oDiv.style.left = 200;
oDiv.style.background = '#FFFF00';
oDiv.style.visibility = 'visible';
oDiv.innerHTML="123123"
document.body.appendChild(oDiv ......