两个比较有用的Javascript工具函数
1.大家在实际工作中,会写各式各样的赋值语句。
比如最常用的obj.style.display = "none";
如果这样的赋值语句一多,obj.style一排下来都要看晕了
下面我的base.js中的extend函数可以允许用json格式赋值属性甚至是函数句柄
/**
* 扩展函数
* @param target 需要扩展的对象
* @param params 要往target里放的属性和方法
*/
function extend(target, params) {
if (!target) {
target = {};
}
for (var prop in params) {
target[prop] = params[prop];
}
return target;
}
2. 由于ie不完全遵守w3c标准,他的事件模型和别的浏览器不一样。调用的方法也不一样。
如果遇到要为控件动态增加事件。用onclick = function() {}一类的在dom动态创建的时候不一定有效,而且无法绑定多个句柄。下面介绍一个通用的支持全浏览器的绑定事件函数。
在大部分情况下,useCapture用的是false,所以这里干脆写死。
/**
* 动态创建事件句柄
* @param control 需要扩展的对象
* @param eventName 事件名
* @param fn 函数句柄
*/
function addEventListener(control, eventName, fn) {
if (window.attachEvent) {
control.attachEvent('on' + eventName, fn);
} else {
control.addEventListener(eventName, fn, false);
}
}
以上两个函数的使用Sample可以参照我写的其他的文章中的代码
相关文档:
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 = doc ......
JavaScript API
One of the new features we added to the ASP.Net Report Viewer in Visual Studio 2010 is a JavaScript API to allow you to interact with the viewer on client. In reading many of the posts on the report controls forum, we found that many people struggle when implementing a custom ......
http://bei123wang.blog.163.com/blog/static/23175492009113022048840/
JavaScript 对象字面量
javascript 2009-12-30 14:20:48 阅读7 评论0 字号:大中小
JavaScript 对象字面量
在编程语言中,字面量是一种表示值的记法。例如,"Hello, World!" 在许多语言中都表示一个字符串字面量(string literal ),JavaScript ......
<html>
<head>
<script language="javascript">
function MyClick() ......