Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在自定义行为脚本初始化之前,jQuery已准备就绪 背景_Javascript_Jquery_Internet Explorer_Javascript Events_Html Components - Fatal编程技术网

Javascript 在自定义行为脚本初始化之前,jQuery已准备就绪 背景

Javascript 在自定义行为脚本初始化之前,jQuery已准备就绪 背景,javascript,jquery,internet-explorer,javascript-events,html-components,Javascript,Jquery,Internet Explorer,Javascript Events,Html Components,我继承了一个古老的web应用程序,该应用程序的输入控件具有使用老式HTC(HTML组件)脚本定义的自定义行为,例如: <input name="txtFiscalYearEndDay" type="text" value="30" maxlength="2" size="5" id="txtFiscalYearEndDay" class="Text1" style="behavior:url(/path/js/InFocus.htc);" /> 以及

我继承了一个古老的web应用程序,该应用程序的输入控件具有使用老式HTC(HTML组件)脚本定义的自定义行为,例如:

<input name="txtFiscalYearEndDay" type="text" value="30" 
    maxlength="2" size="5" id="txtFiscalYearEndDay" class="Text1"       
    style="behavior:url(/path/js/InFocus.htc);" />
以及验证功能:

function txtFiscalYearEndDay_Validate(el) {
    ...
}
注意:我没有使用
$(“#txtFiscalYearEndDay”)
,因为这样我就无法尝试调用
setValid(true),我也不希望必须执行
$('#txtFiscalYearEndDay')[0]。setValid(true)

问题 在验证函数中的某一点上,我尝试调用元素上的一个方法,该方法由HTC脚本添加:

el.setValid(true);
但是,IE调试器感到悲伤,并抱怨
setValid()
不是一个函数。在调试器中检查它可以确认这一点:

typeof el.setValid // "unknown"
当然,一旦页面完成呈现(或者文档实际准备就绪所需的任何时间都已过),验证函数将按预期工作(因为我也在更改和模糊事件上调用相同的验证函数)。也就是说,当在jQuery的on-DOM就绪函数之外调用该函数时,它可以正常工作

你们当中有人对这里可能发生的事情有什么想法吗?jQuery的“ondomready”是在HTC脚本的“ondomready”之前注册的吗?我能改变一下顺序吗?

我目前在IE的所有版本中都看到了这种行为

编辑:解决方法

我发现了一个解决办法。如果您将函数调用从jqueryready函数中取出,并将其扔到页面的末尾,它就会工作(即:)

。。。
txtFiscalYearEndDay_Validate(document.getElementById('txtFiscalYearEndDay');

我不知道HTC是否计入PageReady,但我怀疑他们没有

你可以尝试的是检查一些只有在HTC hase完成后才是tru的东西

然后,您自己的脚本应该启动如下内容:

function MyFunction() {

 if(!HTCIsreadyTest()) {
    setTimeout(MyFunction, 100);
return;
}

//the rest of your code

}
这基本上使您可以在100毫秒内检查功能,如果在测试成功之前不满足条件,则可以重新启动


如果HTC sciprts在2秒后未加载,您还可以为每次尝试超时代码触发器添加一个反参数,将其增加1。我能找到的最简单的解决方法是将验证函数调用移出jQuery
ready()
回调,并将其移到页面末尾:

...
    <script type="text/javascript">
        txtFiscalYearEndDay_Validate(document.getElementById('txtFiscalYearEndDay'));
    </script>
</body>
</html>
我使用的是后者,所以我可以将所有的JS代码放在一起

...
    <script type="text/javascript">
        txtFiscalYearEndDay_Validate(document.getElementById('txtFiscalYearEndDay'));
    </script>
</body>
</html>
function MyFunction() {

 if(!HTCIsreadyTest()) {
    setTimeout(MyFunction, 100);
return;
}

//the rest of your code

}
...
    <script type="text/javascript">
        txtFiscalYearEndDay_Validate(document.getElementById('txtFiscalYearEndDay'));
    </script>
</body>
</html>
$(window).load(function() { // instead of $(function() {
    txtFiscalYearEndDay_Validate(document.getElementById('txtFiscalYearEndDay'));
});