2

と の 2 つの JSP ページを作成outerPage.jspinnerPage.jspました。が含まれてい
ます。 には、1 つのテキストフィールドと 1 つのボタンがあります。outerPage.jspinnerPage.jsp
innerPage.jsp

innerPage.jspページの読み込み中に textFiled にフォーカスを設定する必要があります。
の body onload 時に呼ばれる JavaScript を書いたのですが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

When you have this type of problem,

Just call the script at the end of the page before tag

Your body tag will be changed to

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

スクリプトのsubViewIDがありません:

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

したがって、次のスクリプトを使用して確認します

関数cursorFocus()
{

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

}

于 2010-05-19T02:39:56.517 に答える
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 は信じられないほど不正な形式であるため、Web ブラウザーは、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 関数の機能に集中できます。<form>生成された HTML ソース (Web ブラウザでページを右クリックし、[ソースを表示] ) で要素の生成されたクライアント ID を確認し、 document.getElementById().

于 2010-05-18T11:29:43.570 に答える