Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

2009-04-11

动态创建 style 节点

在 IE 中 DOM 方式创建页面内 style 节点有点问题~ (创建出来的东西是不能修改内容的)
可用 document.createStyleSheet() 来解决:

// create style node
var _styleNode = _(u.STYLE_ID);
if (!_styleNode) {
 if (/*@cc_on!@*/0) { // is IE
  var ss = document.createStyleSheet();
  ss.cssText = _defaultStyleText;
  _styleNode = ss.owningElement; // the style node just created
  _styleNode.id = u.STYLE_ID;
 } else { // other browsers
  _styleNode = document.createElement("style");
  _styleNode.id = u.STYLE_ID;
  _styleNode.type = "text/css";
  _styleNode.innerHTML = _defaultStyleText;
  var head = document.getElementsByTagName("head")[0] || document.body;
  head.appendChild(_styleNode);
 }
}

另外,对于 link 引入 css 可用:
document.createStyleSheet("style.css");
对应:
var style = document.createElement("link");
style.href = "style.css";
style.rel = "stylesheet";
style.type = "text/css";
document.getElementsByTagName('head')[0].appendChild(style);

//EOF Read More...

2008-12-07

ASP.NET TreeView check childs/parents

給 ASP.NET 的 TreeView 寫了一個掛件,主要功能就是在 TreeView 是 ShowCheckBox 模式的時候,當勾選某個節點時能自動的勾選這個節點相關的所有父節點,以及勾選、反勾選這個節點的所有子節點。

功能很簡單純 javascript 實現:

/*************************************************************************
 * change the tree view node's child nodes check-state, when check
 * this node.
 *
 *   NOTE: for ASP.NET TreeView
 *
 * @author h_Davy
 * @version 2008-12-5
 *************************************************************************/

/**
 * check the tree node's childs with this checkbox's stats
 *
 * @param chk   the check box element
 *
 * @private
 */
function _checkTreeChilds(chk)
{
    var childId = chk.id.replace('CheckBox', 'Nodes');
    var child = document.getElementById(childId);
    if (child) {
        var chks = child.getElementsByTagName('input');
        for (var i in chks) {
            if (chks[i].type == "checkbox") {
                chks[i].checked = chk.checked;
            }
        }
    }
}

/**
 * find out the checkbox-node's parent node's checkbox id
 *
 * @param chk       the check box element
 * @param treeView  the treeView element
 *
 * @return  the parent node checkbox id
 *
 * @private
 */
function _findTreeParentId(chk, treeView)
{
    if (!chk) return null;
    var tmpElm = chk.parentNode;
    for(;;) {
        if (tmpElm.tagName == "TABLE") {
            return tmpElm.parentNode.id.replace('Nodes', 'CheckBox');
        } else {
        // go up to find out the parent node, until touch the treeview
            tmpElm = tmpElm.parentNode;
            if (tmpElm == treeView) return null;
        }
    }
    return null;
}

/**
 * check the tree node's parent w this checkbox is checked
 *
 * @param chk   the check box element
 * @param treeView  the treeView element
 *
 * @private
 */
function _checkTreeParent(chk, treeView)
{
    if (!chk.checked) return;
    var tmpChk;
    var parentId = _findTreeParentId(chk);
    for(;;) {
        if (!parentId) return;
        if (chk.checked) {
            tmpChk = document.getElementById(parentId);
            if (!tmpChk || tmpChk == treeView) return;
            tmpChk.checked = true;
        }
        parentId = _findTreeParentId(tmpChk, treeView);
    }
}

/**
 * use this bind to a tree view
 *
 * @param id    the tree view's id (NOTE: it's the HTML node id, not .NET page server id)
 */
function bindCheckEvent(id) {
    var treeView = document.getElementById(id);
    var chks = treeView.getElementsByTagName('input');
    for(var i in chks) {
        if (chks[i].type == "checkbox") {
            chks[i].onclick = function() {
                _checkTreeChilds(this);
                _checkTreeParent(this, treeView);
            };
        }
    }
}

// -- EOF --

使用方法,在要使用的頁面導入以上代碼后,在頁面結尾處加上:
<script type="text/javascript">

bindCheckEvent('TreeView1');
// 這里的 'TreeView1' 就是那個TreeView的ID
// !!!注意:這里的ID指的是最終生成的HTML頁面中的ID,
//                 如果你的TreeView是在某個使用了模板頁的內容頁里面的話,
//                 就有可能是 'ctl00_ContentPlaceHolder1_TreeView1' 了。

</script>

//EOF Read More...

2008-11-02

javascript 中判断是否为IE的技巧

利用 IE 中 jscript/javascript 的“条件编译”
条件编译:
/*@cc_on, @if, @elif, @else, @end, @*/
如:

/*@cc_on
   @if (@_win32)
      document.write("操作系统是32位windows。浏览器是IE。");
   @else
      document.write("操作系统不是32位windows。浏览器是IE。"); // 这个是不会输出的
   @end
@*/
技巧:
    if (!/*@cc_on!@*/0) { //if not IE
        //Firefox2、Firefox3、Safari3.1+、Opera9.6+ support 
    } else {
        //IE6、IE7 support 
    }

//EOF Read More...