【问题标题】:$(document).ready(function() does not work perfectly in Content Pages / Telerik Controls$(document).ready(function() 在 Content Pages / Telerik Controls 中无法完美运行
【发布时间】:2010-07-22 08:12:18
【问题描述】:

我项目中的页面基于母版页和内容页...

我想在之后的一个内容页面中使用 javascript(而不是 jquery)做一些事情 所有主元素和内容元素都已完全加载。(例如,将焦点设置在 RadComboBox 控件上)

为此,我使用了以下代码:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

    <script src="../JQuery/jquery-1.4.1.js" language="javascript" type="text/javascript"></script>


    <script type="text/javascript">

        onload = onloadOfDocument;

        function onloadOfDocument() {

            var combo = $find("<%= RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID %>");
            alert(combo);
            var comboInput = combo.get_inputDomElement();
            comboInput.focus();
        }

    </script>
</asp:Content>

但是alert(combo); 总是返回null。($find 代码是用于telerik 控件的,上面关于telerik 控件的代码是完全正确的)

为了解决这个null 问题,我测试了如下所示的方法:

1- 我从主页面和内容页面中删除了除 RadComboBox 控件和 null 问题之外的所有控件,因此我得出 null 问题是关于主页面和内容页面的所有元素都没有被加载时

$find("&lt;%= RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID %&gt;");

被解雇了。

2- 所以我用了

$(document).ready(function() { my codes });

而不是

onload = onloadOfDocument;

但问题没有解决 - 我不知道为什么!!!

3- 最后我测试了下面的代码,它运行良好:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

<script src="../JQuery/jquery-1.4.1.js" language="javascript" type="text/javascript"></script>


<script type="text/javascript">

    //onload = onloadOfDocument;
      document.onkeyup = onkeyupOfDocument;

    function onkeyupOfDocument() {

        var combo = $find("<%= RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID %>");
        alert(combo);
        var comboInput = combo.get_inputDomElement();
        comboInput.focus();
    }

</script>

我应该使用文档的什么函数来做一些javascript代码在所有的主和内容元素都完全加载之后

提前致谢

【问题讨论】:

标签: javascript asp.net jquery null document-ready


【解决方案1】:

以下链接解决了我的问题: $(document).ready() and pageLoad() are not the same!

我的答案 -> 使用 pageLoad() 而不是 $(document).ready()

【讨论】:

    【解决方案2】:

    会不会:

    var combo=$("#<%= RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID %>")[0];
    

    ?

    (假设RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID解析为DOM中某个元素的id属性)

    【讨论】:

    • 这也是我认为问题所在。检查 $(document).ready(function(){ }); 的最简单方法正在工作是添加一个警报......然后添加您想要在其中运行的代码。它提供了一种简单而干净的方法来确定是否正在加载 jQuery。
    • var combo=$("#")[0]; -> 这个语法不正确... ============= i test $(document).ready(function(){ });使用 alert 和 alert 可以正常工作 - 但 $find 仍然返回 null...
    • 嗯。为什么不正确?你想做什么?获取 ID 为 RadcbPersonelCompleteNameInvwNoskhehEdit.ClientID 的元素,还是我理解错了?我以前从未见过 $find。
    • 我的问题中上层代码(关于 Telerik 控件)的语法是正确的 -> 参考 #3th test... 似乎问题与 $(document).ready(function(){ } );但我用警报测试它,警报工作正常 - 我不知道 document.ready 发生了什么!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2011-04-01
    • 1970-01-01
    • 2011-05-22
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多