javascript - java脚本错误对象不支持此属性或方法

标签 javascript jsf-2

由于某种原因,只有在 startSpin() 函数内定义的微调器对象才能工作。

这是不起作用的代码:

<script type="text/javascript" src="resources/js/spin.js"></script>
    <script type="text/javascript">
        var opts = {
            lines: 18, // The number of lines to draw 
            length: 40, // The length of each line
            top: 'auto', // Top position relative to parent in px
            left: 'auto' // Left position relative to parent in px
        };

         // -- not support ? 
        var target = document.getElementById('spin');
        var spinner = new Spinner(opts).spin(target);
        // -- ???
        function startSpin()
        {
            spinner.start();   
        }
        function stopSpin()
        {
            spinner.stop();  
        }

        function showStatus() {
            startSpin();
            statusDialog.show();
        }

        function hideStatus() {
            stopSpin();
            statusDialog.hide();
        }
    </script>
    <h:form id="testfm">
        <p:commandButton id="start" type="submit" 
                         ajax="false" 
                         value="test" 
                         actionListener="#{bean.test}" 
                         onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"/>
        <p:dialog modal="true" 
                  widgetVar="statusDialog" 
                  showHeader="false"
                  draggable="false" 
                  closable="false" 
                  resizable="false">
            <div id="spin" class="spinner"/>
        </p:dialog>
    </h:form>

微调器仅在 spinStart 函数内定义时才工作

我尝试使用脚本位置,但仍然收到相同的消息 知道为什么吗?

谢谢

最佳答案

你你跑document.getElementById('spin')在该函数之外,具有 id=spin 的元素尚未创建,因此您为微调器指定了空值。如果您在 startSpin 内创建它,它是对用户点击事件的响应,因此 DOM 可能已在此时构建并且该元素存在。这是一个解决方法:

    var spinner;  //Lave the variable out here so both functions can see it

    function startSpin() {
        var target = document.getElementById('spin');
        spinner = new Spinner(opts).spin(target);  //Actually create it here, when the element exists
        spinner.start();   
    }
    function stopSpin() {
        spinner.stop();  
    }

您也可以保留代码不变,并将其放在文档的末尾,就在 </body> 之前。标签。

关于javascript - java脚本错误对象不支持此属性或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623107/

相关文章:

javascript - 数据表createdRow函数未将id设置为行元素

javascript - 如何获取列表元素的数据属性?

javascript - 数据表导出为 pdf 和 excel(带图像)

jsf - 如何扩展从@FacesComponent 调度的 AjaxBehaviorEvent?

jsf-2 - 将参数传递给 JSF 操作

javascript - 将徽章添加到 angular prime ng split button 下拉列表

javascript - 密码字段仅在输入用户名后才显示?

jsf-2 - PrimeFaces 计划日期错误

jsf - 将 CDI bean 注入(inject) JSF @ViewScoped bean

jsf-2 - 无法动态设置组件标识符