4

私たちのdrupalサイトは、アップグレードしていないjQueryバージョン1.2.1で実行されます。

問題はこれです:

jQuery Tokeninputという名前の新しいプラグインを追加する必要がありますが、これは最新のjQueryバージョンでのみ機能します。古いバージョンで最新のjQueryバージョンを追加しようとしましたが、奇妙な結果が生成されます。

私の質問は、古いjQueryプラグインに影響を与えずに最新のjQueryファイルを含める方法です。

4

3 に答える 3

8

方法 #1: (推奨)

次のようなことができます。

<script type='text/javascript' src='js/jquery_1.7.1.js'></script>   
<script type='text/javascript'>  
 // In case you wonder why we pass the "true" parameter,
 // here is the explanation:
 //   - When you use jQuery.noConflict(), it deletes
 //     the "$" global variable.
 //   - When you use jQuery.noConflict(true), it also
 //     deletes the "jQuery" global variable.
 var $jq = jQuery.noConflict(true);  
</script>  
<script type='text/javascript' src='js/jquery_1.2.1.js'></script> 

$そして、 useの代わりに新しいバージョンの jquery で何かを作成したい場合は、このようにします$jq

$jq('.selector').on('click', function(){  
    //do something  
});

方法 #2: (サイトで問題が発生する可能性があります - 推奨されません)

あなたのtemplate.phpファイルで:

<?php
function {theme_name}_preprocess(&$vars, $hook) {
if (arg(0) != 'admin' && $hook == "page") {
// Get an array of all JavaScripts that have been added
$javascript = drupal_add_js(NULL, NULL, 'header');

// Remove the original jQuery library
unset($javascript['core']['misc/jquery.js']);

// Add in our new jQuery library
// We do it this way to keep the includes in the same order
$core = array(
//Alternative jQuery
drupal_get_path('theme', '{theme_name}').'/js/libs/jquery-1.7.1.min.js' => array(
'cache' => TRUE,
'defer' => FALSE,
)
);

// Merge back into the array of core JavaScripts
$javascript['core'] = array_merge($javascript['core'], $core);

// Rerender the block of JavaScripts
$vars['scripts'] = drupal_get_js(NULL, $javascript);
}
}

サイトのフロントエンドでのみこれを行うようにしてください。Drupal のバージョンの jQuery に依存している場合、管理ツールバーが台無しになる可能性があります。

于 2012-06-06T13:35:21.373 に答える
0

Just so you know, there is a jQuery update module which will update jQuery versions for you without you having to code it. Look into it if it's of interest but I don't think that the module keeps the old jQuery version but rather replaces it with the new one.

I might also suggest that you try upgrading the version of jQuery and just using the latest one to see if it does indeed break other functionalities.

于 2012-06-06T14:08:12.200 に答える