Я хочу, чтобы тег Div был пустым, если ширина браузера> 760 пикселей и меньше 980 пикселей, но похоже, что я делаю что-то неправильно
(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);
Любая помощь будет оценена
Thanx
Ваш сценарий в порядке, проблема заключается в том, как вы создаете $('#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);
Попробуйте это в другом заявлении:
else
{
var divString = '<div id="ahead"><div class="column">'+
'Cash On Delivery'+
'</div></div>';
$('#chwd').html(divString);
}
Надеюсь это поможет.
Измените это на другое:
else{
$('#chwd').html('<div id="ahead"><div class="column">Cash On Delivery</div></div>');
}
Использовать outerWidth
width
вместо width
, она включает в себя прокладку и границу. Вам также нужно обернуть div
в "", иначе html()
будет считать его переменной.