Javascript - 设置源文件并为控件赋值

时间:2010-10-08 06:47:17

标签: javascript treeview

我动态地建立了树视图控件。我想改变所选节点的颜色  .one帮我写下面给出的脚本。并且工作正常。

 <script type="text/javascript"  src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("jquery", "1.4.2");
    google.setOnLoadCallback(function() {
        //change cursor to hand when user mouseover tree nodes
        $(".TreeView1_0").mouseover(function() {
            $(this).css('cursor', 'pointer');
        });


        //unbold all nodes then bold the selected node to indicate it's selected
        $(".TreeView1_0").click(function() {
            $(".TreeView1_0").css('font-weight', 'normal');
            $(".TreeView1_0").css('color', 'black');
            $(".TreeView1_0").css('background-color', 'white');


            $(this).css('color', 'white');

            $(this).css("background-color", "blue");

        });
    });
</script>

现在我想将源文件更改为存储在脚本文件夹中的js文件。并将所选节点索引和值存储在隐藏字段中.i在脚本文件夹中有源文件JQuery1.4.1.js。我不知道引用这个js文件的确切方法是什么。我不知道如何检索选定的节点索引和值

我更改了代码来执行此操作。整个aspx代码如下所示

&lt;%@ Page Language =“C#”AutoEventWireup =“true”CodeFile =“Default.aspx.cs”Inherits =“_ Default”%&gt; &lt;!DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Transitional // EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

&lt; html xmlns =“http://www.w3.org/1999/xhtml”&gt; &lt; head id =“Head1”runat =“server”&gt;     &LT;标题&GT;&LT; /标题&GT;

   <script type="text/javascript" src="../../Scripts/JQuery1.4.1.js"></script>
  <script type="text/javascript">


        //change cursor to hand when user mouseover tree nodes
        $(".TreeView1_0").mouseover(function() {
            $(this).css('cursor', 'pointer');
        });


        //unbold all nodes ,then bold the selected node to indicate it's selected ,and store  selected node index and value to two hidden fields
        $(".TreeView1_0").click(function() {
            $(".TreeView1_0").css('font-weight', 'normal');
            $(".TreeView1_0").css('color', 'black');
            $(".TreeView1_0").css('background-color', 'white');
            $(this).css('color', 'white');
            $(this).css("background-color", "blue");

          // i am not sure about the two lines of code given below
            document.getElementById('hfvalue').value = $(this).value;
            document.getElementById('hfindex').value =$(this).index;

            $(this).css('color', 'white');

            $(this).css("background-color", "blue");

        });

</script>

&LT; /头&GT;

&LT;身体GT;

<form id="form1" runat="server">
<div>
    <asp:TreeView ID="TreeView1" runat="server">
    </asp:TreeView>
</div>
<p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:HiddenField ID="hfvalue" runat="server" />
    <asp:HiddenField ID="hfindex" runat="server" />
</p>
</form>

&LT; /体&GT; &LT; / HTML&GT;

但代码无效。我是新手。任何建议

1 个答案:

答案 0 :(得分:1)

我想,首先你需要在脚本中进行一些小改动。请查看以下代码段以使用document.ready -

  

(文档)$。就绪(函数(){

   //change cursor to hand when user mouseover tree nodes

   $(".TreeView1_0").mouseover(function() {

       $(this).css('cursor', 'pointer');

   });

   //unbold all nodes ,then bold the selected node to indicate it's selected ,and store  selected node index and value to two hidden fields

   $(".TreeView1_0").click(function() {

       $(".TreeView1_0").css('font-weight', 'normal');

       $(".TreeView1_0").css('color', 'black');

       $(".TreeView1_0").css('background-color', 'white');

       $(this).css('color', 'white');

       $(this).css("background-color", "blue");

     // i am not sure about the two lines of code given below

       document.getElementById('hfvalue').value = $(this).value;

       document.getElementById('hfindex').value =$(this).index;

       $(this).css('color', 'white');

       $(this).css("background-color", "blue");

   });
     

});

另外,请确保正确选取jquery脚本,并且正在使用的路径是合适的。验证这一点的一种简单方法是在地址栏中键入以下内容,并查看警报是否显示功能

  

的javascript:警报($)