0

ajax でブロックを自動更新するためにこのスニペットを使い始めると、buddypress での「いいね」アクティビティで競合が発生します。

myscr.js

jQuery( document ).ready( function() {
    function update() {
      jQuery("#notice").html('Updating...'); 
      jQuery.ajax({
        type: 'GET',
        url: 'http://domain.com/activity',
        data: "recentac=true",
        //timeout: 5000,
        success: function(data) {
          jQuery("#recent-activities").html(data);
          jQuery("#notice").html(''); 
          window.setTimeout(update, 20000);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          jQuery("#notice").html('Error in connection');
          window.setTimeout(update, 5000);
        }
    });
    }
    update();
});

そして、私はwp_enqueue_script自分のスクリプトを印刷するために使用します:

function auto_refresh()
{
    wp_enqueue_script('myscr', get_template_directory_uri().'/myscr.js', array("jquery"), '1.0', true );
}
add_action('wp_enqueue_scripts', 'auto_refresh', 99);

自動参照が機能し、自動更新の前に「いいね」が機能し、その後は機能しないことに気付きました! また、コンソールにはそれに関するエラーは表示されません。

どんな助けでも大歓迎です。

4

1 に答える 1

0

これが役立つかどうかはわかりませんが、いくつかの競合がないことを読みました。リンクの下にある内容は次のとおりです。ほとんどのコンテンツをコピーしました。リンクには追加の特典がありますので、クリックして詳細を確認してください。お役に立てれば

サイトリンク: http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

jQuery を非競合モードにする jQuery を非競合モードにする場合、新しい変数名を割り当てて $ エイリアスを置き換えるオプションがあります。

<!-- Putting jQuery into no-conflict mode. -->
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>

var $j = jQuery.noConflict();
// $j is now an alias to the jQuery function; creating the new alias is optional.

$j(document).ready(function() {
    $j( "div" ).hide();
});

// The $ variable now has the prototype meaning, which is a shortcut for
// document.getElementById(). mainDiv below is a DOM element, not a jQuery object.
window.onload = function() {
    var mainDiv = $( "main" );
}

</script>

上記のコードでは、$ は元のライブラリの意味に戻ります。アプリケーションの残りの部分では、完全な関数名 jQuery と新しいエイリアス $j を引き続き使用できます。新しいエイリアスには、jq、$J、awesomeQuery など、好きな名前を付けることができます。

最後に、完全な jQuery 関数名の代わりに別の名前を定義したくない場合 ($ を使用するのが好きで、他のライブラリの $ メソッドを使用することは気にしない場合) は、別のアプローチを試すことができます: 単に追加するだけです。 jQuery( document ).ready() 関数に渡される引数としての $。これは、非常に簡潔な jQuery コードの利点が必要であるが、他のライブラリとの競合を引き起こしたくない場合に最も頻繁に使用されます。

    <!-- Another way to put jQuery into no-conflict mode. -->
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>

jQuery.noConflict();

jQuery( document ).ready(function( $ ) {
    // You can use the locally-scoped $ in here as an alias to jQuery.
    $( "div" ).hide();
});

// The $ variable in the global scope has the prototype.js meaning.
window.onload = function(){
    var mainDiv = $( "main" );
}

</script>
于 2013-10-22T01:30:46.930 に答える