Aresli’s Blog

Entries for the ‘Javascript’ Category

toString()和toLocaleString()返回的值是不一样的

验证:
var aColors = new Array(”red”, “green”, “blue”);
alert(aColors.toString());
alert(aColors.toLocaleString());
alert(aColors.toString() == aColors.toLocaleString());
不知道为什么toLocaleString()返回的值在逗号分隔符后有一个空格!

Leave a Comment

几个有用的JS函数

 
几个有用的js,可以把这几个函数写成一个common.js首先加载:
 
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != ‘function’) {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
};
}
}
 
 
上面这个函数可以代替window.onload = function() {}
可以不受window.onload只能在一处使用的限制
但在别处使用这个函数,这个js要先加载
██████████████████████████████
function insertAfter(newElement, targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
}
else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
 
 
这个函数是针对javascript只有insertBefore()的补充
newElement将插在targetElement后面
———————————————–
insertBefore调用语法
parentElement.insertBefore(newElement, targetElement)
██████████████████████████████
function addClass(element, value) {
if (!element.className) {
element.className = value;
}
else {
newClassName = element.className;
newClassName += ” “;
newClassName += value;
element.className = newClassName;
}
}
这个函数可以把你写好的css赋值给你取到的元素节点
即把value赋值给element
可以多次赋值,多次赋值不是后面的取代前面,而是效果叠加

Leave a Comment