2

我写了两个 JSP 页面:outerPage.jspinnerPage.jsp. 包括.
_ 有一个文本字段和一个按钮。outerPage.jspinnerPage.jsp
innerPage.jsp

我需要在innerPage.jsp页面加载时将焦点放在 textFiled 上。
我写了 JavaScript,它在 body onload 期间被调用outerPage.jsp,但它不起作用。

这是outerPage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>    

<html>
    <f:view>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Outer Viewer</title>
            <meta name="description" content="Outer Viewer" />                    
        </head>
        <body id="outerMainBody">
            <rich:page id="richPage">                             
                <rich:layout>
                    <rich:layoutPanel position="center" width="100*">
                        <a4j:outputPanel>
                            <f:verbatim><table style="padding: 5px;"><tbody><tr>
                                            <td>
                                               <jsp:include page="innerPage.jsp" flush="true"/>      
                                            </td>
                                        </tr></tbody></table></f:verbatim>
                                </a4j:outputPanel>
                            </rich:layoutPanel>
                        </rich:layout>
                    </rich:page>
        </body>
    </f:view>
</html>

这是innerPage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>   

<f:verbatim><html></f:verbatim>
    <f:subview id="innerViewerSubviewId">
        <f:verbatim><head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>Inner Viewer </title>               
                <script type="text/javascript">
//This script does not called during the page loading (onload)       
                    function cursorFocus() 

                    {
                        alert("Cursor Focuse Method called...");                
                        document.getElementById("innerViewerForm:innerNameField").focus();
                        alert("Cursor Focuse method end!!!");
                    }               
                </script>
            </head>
            <body onload="cursorFocus();"></f:verbatim>

            <h:form id="innerViewerForm">
                <rich:panel id="innerViewerRichPanel">

                    <f:facet name="header">
                        <h:outputText value="Inner Viewer" />
                    </f:facet>

                    <a4j:outputPanel id="innerViewerOutputPanel" >

                        <h:panelGrid id="innerViewerSearchGrid" columns="2" cellpadding="3" cellspacing="3">

                             //<%-- Row 1 For Text Field --%>
                     <h:outputText value="inner Name : " />
                     <h:inputText id="innerNameField" value=""/>                           

                     //<%--  Row 2 For Test Button --%>
                     <h:outputText value="" />
                     <h:commandButton  value="TestButton" action="test" />

                    </h:panelGrid>                                             

                    </a4j:outputPanel>
                </rich:panel>
            </h:form>           
            <f:verbatim></body></f:verbatim>
    </f:subview>
    <f:verbatim></html></f:verbatim>

cursorFocus()脚本不会被调用。

提前致谢。

4

3 回答 3

1

当您遇到此类问题时,

只需在标签之前调用页面末尾的脚本

您的身体标签将更改为

...content above body
<body>
...content inside body
<script type="text/javascript">cursorFocus();</script>
</body>
...content after body
于 2010-05-18T06:13:26.200 回答
0

您的 HTML 在语法上无效。生成的 HTML 输出必须如下所示:

<!doctype declaration>
<html>
    <head>...</head>
    <body>...</body>
</html>

但你的结果如下:

<!doctype declaration>
<html>
     <head>...</head>
     <body>
         <!doctype declaration>
         <html>
              <head>...</head>
              <body>...</body>
         </html>
     </body>
</html>

在浏览器中右键单击页面并查看源代码。看起来对吗?不会。如果您已经测试过,W3 标记验证器也应该暗示过这一点。

由于生成的 HTML 格式非常错误,网络浏览器不知道如何以及在何处找到 Javascript 函数中所需的 HTML DOM 元素。行为未指定。

您需要按如下方式重写页面:

outerPage.jsp

<!doctype declaration>
<f:view>
    <html>
        <head>
            <title>...</title>
            <meta>...</meta>
            <script>...</script>
        </head>
        <body>
            <jsp:include />
        </body>
    </html>
</f:view>

innerPage.jsp

<f:subview>
    <h:form>
        <h:inputText />
    </h:form>
</f:subview>

确实,您应该在! _ 只需对其进行编码,就好像它是实际的包含一样,但只有在它周围。<!doctype><html><head><body>innerPage.jsp <f:subview>

这样,它将最终在语法上有效:

<!doctype declaration>
<html>
    <head>
        <title>...</title>
        <meta>...</meta>
        <script>...</script>
    </head>
    <body>
        <form>
            <input type="text" />
        </form>
    </body>
</html>

对齐所有 HTML 后,您就可以专注于 JavaScript 函数的功能。检查生成的 HTML 源代码中元素的生成客户端 ID <form>(webbrowser 中的右键单击页面,查看源代码),然后在document.getElementById().

于 2010-05-18T11:29:43.570 回答
0

您错过了脚本的 subView id:

              function cursorFocus() 
               {
                    alert("Cursor Focuse Method called...");                
                    document.getElementById("innerViewerForm:innerNameField").focus();
                    alert("Cursor Focuse method end!!!");
                }               
            </script>

所以你使用下面的脚本并检查它

功能光标焦点()
{

document.getElementById("innerViewerSubviewId:innerViewerForm:innerNameField").focus();   

}

于 2010-05-19T02:39:56.517 回答