Javascript 移动设备if语句

Javascript 移动设备if语句,javascript,jquery,Javascript,Jquery,我的计划是,如果移动设备宽度小于480,用段落替换html代码中的iframe。我写了以下内容,但它不起作用。有人能分享我做错了什么吗 // Small device if ($(window).width() < 480) { $("#quiz-iframe iframe").replaceWith(function(){ return $('<p>Please refer to Desktop or Tablet for the content.

我的计划是,如果移动设备宽度小于480,用段落替换html代码中的iframe。我写了以下内容,但它不起作用。有人能分享我做错了什么吗

// Small device

 if ($(window).width() < 480)  {

  $("#quiz-iframe iframe").replaceWith(function(){
        return $('<p>Please refer to Desktop or Tablet for the content.</p>');
    });

}
//小型设备
如果($(窗口).width()<480){
$(“#quick iframe iframe”).replaceWith(function(){
return$('有关内容,请参阅桌面或平板电脑。

); }); }
感谢您的帮助。

试试:

if ($(window).width() < 480)  {

    $("#quiz-iframe iframe")
        .replaceWith('<p>Please refer to Desktop or Tablet for the content.</p>');
}
if($(窗口).width()<480){
$(“#测验iframe iframe”)
.替换为(“请参阅桌面或平板电脑了解内容。

”); }
试试这个

$(window).resize(function() {
    var width = $(window).width();
    if(width < 480)
    {
        //do what you want here.
    }
});
$(窗口)。调整大小(函数(){
变量宽度=$(窗口).width();
如果(宽度<480)
{
//在这里做你想做的。
}
});

谢谢大家。我使用了resize函数,它按预期工作。谢谢大家!

// Small device

$(window).resize(function() {
    var width = $(window).width();
if(width < 480)
{
    $("#quiz-iframe iframe").replaceWith(function(){
        return $('<p>Please refer to Desktop or Tablet for quiz content.</p>');
    });
}
});
//小型设备
$(窗口)。调整大小(函数(){
变量宽度=$(窗口).width();
如果(宽度<480)
{
$(“#quick iframe iframe”).replaceWith(function(){
return$(“有关测验内容,请参阅桌面或平板电脑。

”); }); } });
试试-

if ($(window).width() < 480)  {

    $("#quiz-iframe iframe").html("<p>Please refer to Desktop or Tablet for the content.</p>");

}
if($(窗口).width()<480){
$(“#quick-iframe-iframe”).html(“有关内容,请参阅桌面或平板电脑。

”; }
这将如何解决他的问题?我同意在他的情况下使用a函数是不必要的,但它应该仍然有效,不是吗?@Barmar我不这么认为。也许只有我一个人,但我不知道他的代码返回值中的$是用来做什么的。
$(“html代码”)
创建了一个jQuery对象,其中包含使用该html创建的元素。就像在编写
$(“#表”).append($(“”)好的,但我看不出使用它的意义。在我看来,这只是多余的。另外,我从来没有使用过replaceWith(),我只是修复了一个我认为需要从他的代码中修复的部分。如果我在某些方面错了,请随意提出另一种解决方案。为什么不使用媒体查询?它以什么方式不起作用?如果他在移动设备上,他不必调整窗口大小以使其变小,当页面加载时它已经很小了。@Barmar是的<代码>调整大小
是不需要的,但如果他试图开发的网站可以在浏览器中访问,我就在那里添加了它。。。。