-4

編集: jquery ライブラリをロードすることで機能しました。jQuery は何もインポートしなくても JavaScript のように機能すると思いました。初心者の質問で時間を無駄にして申し訳ありません。

///

これは、jQuery を使用して IE6 のトップ メニュー (ホバー効果) の表示を修正する wordpress プラグインです。

私が望むのは、プラグインの必要性を排除しながら、テーマに機能を実装することです。

この部分を入れた場合に発生していたエラー

<script type="text/javascript">
    jQuery(document).ready( function($) {
     $('#access li').mouseover( function() {
     $(this).find('ul').show();
     });
     $('#access li').mouseleave( function() {
     $(this).find('ul').hide();
     });
     $('#access li ul').mouseleave( function() {
     $(this).hide();
     });
    });
    </script>

この<head>セクションではUncaught ReferenceError: jQuery is not defined、JS が読み込まれる前にコードが実行されることが問題である可能性が高いことがわかりました。

Webで見つかったソリューションを使用し、コードをこれにラップしました

$(document).ready(function () {
  //my code here
});

試してみましたが、2つのエラーが発生しました。

Uncaught ReferenceError: $ is not defined
Uncaught ReferenceError: jQuery is not defined

エラーを見た後、より明確になりましたが、なぜ機能しないのかまだわかりません

///

http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.jsからjqueryライブラリをロードすることで機能しましたjQueryは何もインポートする必要なくJavaScriptのように機能すると思いました;D

Wordpress プラグインのソース コード:

<?php
/**
 * Plugin Name: Twentyten IE6 Menus
 * Author: Sara Cannon
 * Author URI: http://sara-cannon.com
 * Description: Make the menu drop down in IE6 (if you care about that sort of thing)
 * Version: 1.0
 * License: GPL2
 */
function sara_twentyten_ie6_menus_enqueue() {
 wp_enqueue_script( 'jquery' );
}
add_action( 'after_setup_theme', 'sara_twentyten_ie6_menus_enqueue' );

function sara_twentyten_ie6_menus_script() {
?>
<!--[if lte IE 6]>
<script type="text/javascript">
jQuery(document).ready( function($) {
 $('#access li').mouseover( function() {
 $(this).find('ul').show();
 });
 $('#access li').mouseleave( function() {
 $(this).find('ul').hide();
 });
 $('#access li ul').mouseleave( function() {
 $(this).hide();
 });
});
</script>
<![endif]-->
<?php
}
add_action( 'wp_footer', 'sara_twentyten_ie6_menus_script' );
4

1 に答える 1

0

jQuery は .ready(function() {}); に何も渡さないでください。

これを試して:

    (function($, window, document, undefined) {
       $(function() {
         $('#access li').mouseover( function() {
         $(this).find('ul').show();
         });
         $('#access li').mouseleave( function() {
         $(this).find('ul').hide();
         });
         $('#access li ul').mouseleave( function() {
         $(this).hide();
         });
       });
    })(jQuery, window, document);

jQuery を正しくインポートした場合、このブロック内で jQuery ($) を未定義にしないでください。

于 2012-05-17T03:40:40.513 に答える