Javascript Jquery将根据浏览器宽度进行更改
如果浏览器宽度>760 px且小于980 px,我希望Div标记为空,但看起来我做错了什么Javascript Jquery将根据浏览器宽度进行更改,javascript,jquery,css,width,browser-width,Javascript,Jquery,Css,Width,Browser Width,如果浏览器宽度>760 px且小于980 px,我希望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 < 98
(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使用外层宽度而不是宽度,它包括填充和边框。您还需要包装div,否则html将假定它是一个变量。您的脚本很好,问题在于如何构造$'chwd'.html Javascript以不同的方式处理新行。与PHP或其他语言不同,PHP或其他语言允许您继续放置新行,只要它用引号/单引号包装。在Javascript中,每次你做一行新代码时,你都必须用+来包装它并查看我的代码 这是可以接受的 这是不能接受的 试试这个:
将您的其他内容更改为:
else{
$('#chwd').html('<div id="ahead"><div class="column">Cash On Delivery</div></div>');
}
在else语句中尝试以下操作:
else
{
var divString = '<div id="ahead"><div class="column">'+
'Cash On Delivery'+
'</div></div>';
$('#chwd').html(divString);
}
希望这有帮助。您的宽度是否正确?你得到的结果是什么?当你说空的时候,你的意思是你想删除内容吗?或者干脆把它藏起来?@Anamika是的,我得到的宽度是正确的,但只有当删除else语句时,它似乎对else不起作用statement@StephanusYanaputra:-如果div id的内容变为null或不为空,请不要忘记选择答案@angadarora,这样主题就正确标记了我看到了,真是太好笑了:
(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{
$('#chwd').html('<div id="ahead"><div class="column">Cash On Delivery</div></div>');
}
else
{
var divString = '<div id="ahead"><div class="column">'+
'Cash On Delivery'+
'</div></div>';
$('#chwd').html(divString);
}