首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在JSF2.0中使用jQuery

如何在JSF2.0中使用jQuery
EN

Stack Overflow用户
提问于 2011-12-29 20:38:58
回答 1查看 56.5K关注 0票数 21

我正在学习jQuery。我想在我的jSF2.0应用程序中使用jQuery。例如,我有一个html文件,其中我使用的jQuery如下所示

<head>
    <title>The Devil's 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官方网站下载了jquery-1.7.1.js文件,将其包含在我的目录中,然后开始使用jQuery。

我的06.js文件包含如下代码

$(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's 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将以相同的方式工作,或者我必须下载一些插件才能在我的JSF2.0应用程序中使用jQuery?或者在我的web.xml中修改一些东西?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-29 21:04:24

它是以同样的方式工作的。

ID属性

JSF将表单的ID附加到表单中的所有输入字段。因此,您需要小心使用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>
票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8667980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档