JQuery与Primefaces冲突?

Sel*_*vin 34 primefaces jsf-2 jquery-1.5

我在JSF页面的标题中包含了JQuery1.5.在该页面中,已经编码了许多Primefaces组件.在我Jquery.js将页面标题包含在页面的标题之后,一些主要组件就像<p:commandButton>失去了它们的皮肤一样,<p:fileUpload>看起来像普通的JSP <input type="file">并完全失去了它的AJAX功能.

有没有办法安全地使用JQuery和primefaces(没有冲突)?

Lar*_*erg 50

我遇到了与问题中描述的问题相同的问题.这就是我想出以下解决方案的原因:

包括primefaces内置的jQuery库(目前为1.4.1),因为包含一个自己的jQuery库会导致CSS格式化问题.添加target="head"属性允许在任何地方指定标记 - 例如,在使用模板时,您无法始终访问<head>标记:

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />
Run Code Online (Sandbox Code Playgroud)

默认情况下,冲突模式中包含primefaces jQuery库.这意味着$()无法使用快捷方式.要克服此问题,请在a <script><h:outputScript>标记中包含以下行:

<h:outputScript target="head">
    // Add the $() function
    $ = jQuery;
    // Now you can use it
    $(document).ready(function() {
        ...
    });
</h:outputScript>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止可以挖掘的最好的解决方案,使用primefaces 2.2.1.

  • 除了上面的Lars解释之外,Primefaces不会在不使用Primefaces组件的页面上自动包含jQuery,即使在<html>标签中定义了Primefaces名称空间,因此您可以使用上述Lars解释的方法或更改您使用Primefaces组件实现的组件之一,例如将<h:selectOneMenu>更改为<p:selectOneMenu>. (3认同)

Cag*_*ici 43

为什么不在PrimeFaces中使用jquery包呢?

<h:outputScript library="primefaces" name="jquery/jquery.js" />
Run Code Online (Sandbox Code Playgroud)

PrimeFaces 2.2.1有jQuery 1.4.4和3.0(开发中)有1.5.1.

  • 作为旁注,2.2.1的jquery默认启用noconflict,3.0没有noconflict. (4认同)

diE*_*cho 7

许多JavaScript库使用$作为函数或变量名,就像jQuery一样.在jQuery的情况下,$只是jQuery的别名,因此所有功能都可以在不使用$的情况下使用.以下是一些方法:

  • jQuery.noConflict();在jQuery初始化之前写,见下文

    jQuery.noConflict();
    $(document).ready(function(){
       // your jQuery code   
    });
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建一个不同的别名而不是jQuery,以便在脚本的其余部分中使用.

     var j = jQuery.noConflict();
     // Do something with jQuery
     j("div p").hide();
    
    Run Code Online (Sandbox Code Playgroud)
  • 更改所有的实例$:替换$jQueryjQuery的代码块

     jQuery(document).ready(function){
           jQuery("div p").hide();
     })
    
    Run Code Online (Sandbox Code Playgroud)
  • 完全将jQuery移动到另一个对象中的新命名空间.

    var dom = {};
    dom.query = jQuery.noConflict(true);
    // Do something with the new jQuery
    dom.query("div p").hide();
    
    Run Code Online (Sandbox Code Playgroud)
  • $的范围设置为local而不是global

        // Method 1
        jQuery(document).ready(function($){
             $("div").hide();
        });
    
    
        // Method 2
        (function($) {
          /* some code that uses $ */ 
        })(jQuery);
    
    Run Code Online (Sandbox Code Playgroud)

    注意:这一点有一个缺点,您将无法访问其他库的$()方法.

Original Reference


Mar*_*rin 6

我的解决方案是在默认页面中添加此代码:

<script type="text/javascript">var $j = jQuery.noConflict(true);</script>
Run Code Online (Sandbox Code Playgroud)

之后我使用jquery:

$j 
Run Code Online (Sandbox Code Playgroud)

谢谢,