2010-06-15 50 views
0

我有一個樹形視圖,默認情況下有一個文件夾圖標,一旦點擊它就必須更改爲複選框圖標。進一步點擊複選框圖標應顯示一個文件夾圖標。javascript image toggling

示例代碼:


服務器端代碼:C#

htmlSb.AppendFormat("<li><span class=\"folder\" 
    onclick=\"javascript:return Test.Controls.TreeView.SelectNode('" 
    + this.Id 
    + "',this);\">{0}</span></li>", emptyContent); 

JavaScript代碼:

var Test= new Object(); 
Test.Controls=new Object(); 
Test.Controls.TreeView = new Object(); 

Test.Controls.TreeView.SelectNode = function (TreeId, nodeLabel) { 
    $("#" + TreeId + " li span, ul li span").css("background-color", "transparent"); 
    nodeLabel.style.backgroundColor = "white"; 
    nodeLabel.style.background = "url(../images/selected.gif) 0 0 no-repeat"; 
} 

另一圖像:

if (nodeLabel.style.background = "url(../images/folderclosed.gif) 0 0 no-repeat") 

我需要"selected.gif""folderclosed.gif"圖像之間切換。如果點擊了另一個應該顯示。反之亦然。

請幫忙。

+0

對不起亞歷克斯,我是一名新成員,我不知道this.I've目前標誌着接受的答案。 感謝您指出。 – SRA 2010-06-15 06:35:22

回答

1

看起來像你有jQuery提供給你。這應該是訣竅:

// get a jquery object for the node label 
var $nodeLabel = $(nodeLabel); 

if ($nodeLabel.data('background') == '' || $nodeLabel.data('background') == 'folderclosed') { 
    // if the node label has no background data set or is set to folderclosed, set to selected 
    $nodeLabel.data('background', 'selected').css('background', 'url(../images/selected.gif) 0 0 no-repeat'); 
} else { 
    // if the node label is set to selected, set to folderclosed 
    $nodeLabel.data('background', 'folderclosed').css('background', 'url(../images/folderclosed.gif) 0 0 no-repeat'); 
} 
+0

瀏覽器說對象不支持此屬性 $ nodelabel.data。我是否缺少任何jquery js文件?我有jquery.js,jquerytreeview.js,jquerycookie.js被引用 – SRA 2010-06-15 06:46:46

+0

數據函數包含在覈心中,所以除了jquery.js之外,您不需要任何其他功能。不確定你使用的是哪個treeview插件,但是你看看bassistance.de上的插件嗎?它似乎有你正在尋找內置的功能。這裏是鏈接http://bassistance.de/jquery-plugins/jquery-plugin-treeview/ – 2010-06-15 13:12:10