【问题标题】:Javascript - Set source file and assigning value to a controlJavascript - 设置源文件并为控件赋值
【发布时间】:2011-04-22 17:50:05
【问题描述】:

我有一个动态构建的树视图控件。我想更改所选节点的颜色 .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 文件。并将选定的节点索引和值存储在隐藏字段中。我在脚本文件夹中有一个源文件 JQuery1.4.1.js。我不知道引用这个 js 文件的确切方法是什么。而且我不知道如何检索选定的节点索引和值

我更改了代码来做到这一点。整个 aspx 代码如下所示

   <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>

<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>

正文>

但代码不起作用。我是新手。任何建议

【问题讨论】:

    标签: javascript treeview


    【解决方案1】:

    我想,首先你需要对你的脚本做一个小的改动。查看以下 sn-p 以使用 document.ready -

    $(document).ready(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 ,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:alert($)

    【讨论】:

    • 我尝试添加 $(document).ready(function() 。但它给出了一个错误 -->Microsoft JScript 运行时错误:预期对象
    • 这几乎意味着您的脚本路径不正确。您是否按照上面的建议使用 javascript:alert($) 进行了验证?
    • 是的。谢谢。它现在工作。关于如何将所选节点值和索引分配给隐藏字段的任何想法? document.getElementById('hfvalue').value = $(this).value; document.getElementById('hfindex').value =$(this).index;这是正确的方法吗? .谢谢
    • 嘿,我不明白你的问题,但无论如何,如果我的回答解决了你的问题,只是想提醒你,你可能想接受它:)
    • 哈哈。好的。没有得到确切的答案。无论如何我很高兴:)。谢谢你
    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多