2017-02-20 5 views
0

ブラウザウィンドウの幅をonResize()に表示しようとしています。私はそれについての多くの記事を読んだが、まだ動作していない。 JavaScriptは実行されません(アラートは決して表示されません)。onResize()がWordPress 4.7.2で動作しない

このonResize = "dispScreenWidth()"の使用方法は、ブートストラップサイトで正常に動作します。私は、get_template_directory_uri()パスが正しいことを知っています。 jQueryアップデータをインストールしましたが、それは助けにはなりませんでした。

functions.php 
<?php 
    function flashdatadesign_twentytwelve_credits() { 
     echo 'Website by <a href="http://example.com" title="example">';    
    } 
    add_action('twentytwelve_credits', 'mysite_twentytwelve_credits'); 

    wp_enqueue_script('bcscript', get_template_directory_uri().'/js/bcscript.js', array ('jquery'), NULL, true); 
?> 

header.php 
<body <?php body_class(); ?> onResize="dispScreenWidth()"> 
    <div id="page" class="hfeed site"> 
     <p id="diag"></p> // --- this is just to display window.innerWidth 
     <header id="masthead" class="site-header" role="banner"> 
     <hgroup> 
     ........... 
</body> 

bcscript.js 
jQuery(document).ready(function(){ 
    function dispScreenWidth() { 
     jQuery('#diag').css('display', 'inline'); 
     jQuery('#diag').html(''); 
     jQuery('#diag').append(window.innerWidth + ', '); 

     alert('display'); 
    } 
}); 

答えて

1

機能が有効範囲外です。

dispScreenWidth()関数を別の関数内に宣言していますので、onResizeで呼び出すと、その関数は使用できません。

jQuery(document).readyスコープから削除して、メインスコープに入れてみます。このような

何か:

jQuery(document).ready(function(){ 
    //do other stuff 
}); 

function dispScreenWidth() { 
    jQuery('#diag').css('display', 'inline'); 
    jQuery('#diag').html(''); 
    jQuery('#diag').append(window.innerWidth + ', '); 

    alert('display'); 
} 

また、あなたがbodyタグにそれを置くのではなく、さらにonResizeするjQueryの同等のものを使用することができます。このよう

:!

jQuery(document).ready(function(){ 
    //do other stuff 
}); 

jQuery(window).on('resize', function(){ 
    dispScreenWidth(); 
}); 

function dispScreenWidth() { 
    jQuery('#diag').css('display', 'inline'); 
    jQuery('#diag').html(''); 
    jQuery('#diag').append(window.innerWidth + ', '); 

    alert('display'); 
} 
+0

jQueryの(ウィンドウ).on( 'サイズを変更'、関数は()素晴らしい仕事のおかげで – fuey

関連する問題