0

ブラウザーの幅が 760 ピクセル以上で 980 ピクセル未満の場合、Div タグを空にしたいのですが、何か間違ったことをしているように見えます

    (function($){

  //detect the width on page load
  $(document).ready(function(){

    var current_width = $(window).width();
     //do something with the width value here!
    if(current_width < 980 && current_width > 760){
       $('#chwd').empty();
    }
  });

  //update the width value when the browser is resized (useful for devices which switch from portrait to landscape)
  $(window).resize(function(){
    var current_width = $(window).width();
   //do something with the width value here!

    if(current_width < 980 && current_width > 760  ){
         $('#chwd').empty();
    }
    else{
        $('#chwd').html(

<div id="ahead">
<div class="column">
Cash On Delivery
</div>
</div>

        );}
  });

})(jQuery);

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

ありがとう

4

4 に答える 4

0

これをelseステートメントで試してください:

else
{
    var divString = '<div id="ahead"><div class="column">'+
                    'Cash On Delivery'+
                    '</div></div>';
    $('#chwd').html(divString);
}

お役に立てれば。

于 2013-12-06T05:52:18.413 に答える
0

outerWidthの代わりに使用widthします。パディングとボーダーが含まれます。を "" で囲む必要もあります。divそうしないhtml()と、変数であると見なされます。

于 2013-12-06T05:50:25.653 に答える
0

あなたをこれに変えてください:

else{
   $('#chwd').html('<div id="ahead"><div class="column">Cash On Delivery</div></div>');
}
于 2013-12-06T05:52:03.430 に答える
0

あなたのスクリプトは問題ありませんが、問題はあなたがどのように構築するかにあります$('#chwd').html()

Javascript は改行を異なる方法で処理します。引用符/一重引用符で囲まれている限り、改行を続けることができる PHP や他の言語とは異なります。Javascript では、新しい行を作成するたびに、'' と + でラップする必要があります (私のコードを参照してください)。

これは許容範囲です

'Lorem' +
'Ipsum'

これは受け入れられません

'Lorem
 Ipsum'

これを試して:

 (function($){

  //detect the width on page load
  $(document).ready(function(){

    var current_width = $(window).width();
     //do something with the width value here!
    if(current_width < 980 && current_width > 760){
       $('#chwd').empty();
    }
  });

  //update the width value when the browser is resized (useful for devices which switch from portrait to landscape)
  $(window).resize(function(){
    var current_width = $(window).width();
   //do something with the width value here!

    if(current_width < 980 && current_width > 760  ){
         $('#chwd').empty();
    }
    else{
        $('#chwd').html('<div id="ahead">'+
'<div class="column">'+
'Cash On Delivery'+
'</div>'+
'</div>');
    }
  });

})(jQuery);
于 2013-12-06T05:50:37.980 に答える