Подтвердить что ты не робот

Как использовать jQuery с JSF 2.0

Я изучаю jQuery. Я хочу использовать jQuery внутри моего приложения jsf 2.0. Как у меня есть html файл, в котором я использую jQuery вроде этого

<head>
    <title>The Devil Dictionary</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="css/06.css" type="text/css" />

    <script type="text/javascript" src="js/jquery-1.7.1.js"></script>
    <script type="text/javascript" src="js/06.js"></script>
</head>

Я просто загрузил официальный файл jquery-1.7.1.js на официальный сайт jQuery, включил его в свой каталог и затем начал использовать jQuery.

My 06.js файл код conatin, подобный этому

$(document).ready(function() {

    $('#letter-a a').click(function(){

        /**
         * The .load() method does all our heavy lifting for us! We specify the target location for
         * the HTML snippet by using a normal jQuery selector, and then pass the URL of the file to
         * be loaded as a parameter to the method. Now, when the first link is clicked, the file is
         * loaded and placed inside <div id="dictionary">. The browser will render the new HTML as
         * soon as it is inserted,
         */
        $('#dictionary').load('a.html');
        return false;

    }); //end of $('#letter-a a').click()

    /**
     * Occasionally, we don't want to retrieve all the JavaScript we will need when the page is
     * first loaded. We might not know what scripts will be necessary until some user interaction
     * occurs. We could introduce <script> tags on the fly when they are needed, but a more elegant
     * way to inject additional code is to have jQuery load the .js file directly.
     *
     * Pulling in a script is about as simple as loading an HTML fragment. In this case we use
     * the $.getScript() function, which, like its siblings, accepts a URL locating the script
     * file, as shown in the following code snippet:
     */
    $('#letter-c a').click(function(){

        $.getScript('js/c.js');
        return false;

    }); //end of $('#letter-c a').click(function())

}); //end of $(document).ready(fn)

Вот мой фрагмент кода html файла

<html>
<head>
    <title>The Devil Dictionary</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="css/06.css" type="text/css" />

    <script type="text/javascript" src="js/jquery-1.7.1.js"></script>
    <script type="text/javascript" src="js/06.js"></script>
</head>

<body>

    <div id="container">

        <div class="letters">
            <div class="letter" id="letter-a">
                <h3><a href="#">A</a></h3>
            </div>

            <div class="letter" id="letter-b">
                <h3><a href="#">B</a></h3>
            </div>

            <div class="letter" id="letter-c">
                <h3><a href="#">C</a></h3>
            </div>

        </div>

        <div id="dictionary">
        </div>

    </div>

</body>

Я хочу спросить, создаю ли я одну и ту же страницу на JSF2.0, тогда jQuery будет работать одинаково или я должен отключить некоторый плагин для использования jQuery внутри моего приложения JSF 2.0? или изменить что-то внутри моего web.xml?

Спасибо

4b9b3361

Ответ 1

Он работает одинаково.

Атрибуты идентификатора

JSF добавляет идентификатор формы ко всем полям ввода внутри формы. Следовательно, вам нужно быть осторожным с вашими селекторами jQuery. Например, предположим, что у вас есть следующая форма:

<h:form id="myForm">
   <h:inputText id="myInput" />
</h:form>

Используя jQuery, вы должны обращаться к входу как: $("#myForm\\:myInput")

Каркасы

В некоторых средах, таких как PrimeFaces, используются компоненты, которые могут не работать или могут потерять их, если вы импортируете jQuery с помощью:

<script type="text/javascript" src="js/jquery-1.7.1.js"></script>

Вместо этого вы должны использовать свою встроенную библиотеку jQuery, импортировав ее как:

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />

Однако эта библиотека jQuery включена в режим конфликта, что означает, что вы не можете использовать $(). Следовательно, вам может понадобиться эта дополнительная настройка:

<h:outputScript target="head">
    $ = jQuery;
    // Then you can use it
    $(document).ready(function() {
        ...
    });
</h:outputScript>