javascript图片切换

时间:2010-06-15 02:01:21

标签: .net javascript

我有一个树形视图,默认情况下有一个文件夹图标,一旦点击它就必须更改为复选框图标。进一步单击复选框图标应显示文件夹图标。

示例代码:


服务器端代码: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"图片之间切换。如果单击一个,则应显示另一个。反之亦然。

请帮忙。

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:0)

听起来像jQuery切换功能最容易,因为它是专为此而设计的。

http://api.jquery.com/toggle/