Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态更改a的大小<;部门>;要素_Javascript_Html_Function_If Statement - Fatal编程技术网

Javascript 动态更改a的大小<;部门>;要素

Javascript 动态更改a的大小<;部门>;要素,javascript,html,function,if-statement,Javascript,Html,Function,If Statement,当从if语句调用函数时,我试图调整的大小。我知道if语句正在工作,因为当调用函数mTr()时,它工作了,我知道函数myFun()工作了,但由于某种原因,函数myFun()没有被调用 if (window.screen.height==568) { // iPhone 4" document.querySelector("meta[name=viewport]").content="width=320.1"; myFun("divId"); mTr(); } functi

当从if语句调用函数时,我试图调整
的大小。我知道
if
语句正在工作,因为当调用函数
mTr()
时,它工作了,我知道函数
myFun()
工作了,但由于某种原因,函数myFun()没有被调用

if (window.screen.height==568) { // iPhone 4"
    document.querySelector("meta[name=viewport]").content="width=320.1";
    myFun("divId");
    mTr();

}
function myFun(id)
{
    var obj = document.getElementById(id);
    if (obj)
    {
        obj.setAttribute("style", "height:300px;");
    }
}
function mTr()
{
    alert("Hello World!");
}

我重新测试了一切。我想我发现了你的问题。您的代码中有一个错误

此处出现错误:

document.querySelector("meta[name=viewport]").content="width=320.1";
相反,它应该是:

document.querySelector("meta[name=viewport]").setAttribute('content', 'width=320.1');  
if (window.screen.height==568) { 

   document.querySelector("meta[name=viewport]").setAttribute('content', 'width=320.1');    
   myFun("divId");
   mTr();
}

function myFun(id)
{
   var obj = document.getElementById(id);
   if (obj)
   {
       obj.setAttribute("style", "height:300px;");
   }
}

function mTr()
{
   alert("Hello World!");
}
完整的Javascript:

document.querySelector("meta[name=viewport]").setAttribute('content', 'width=320.1');  
if (window.screen.height==568) { 

   document.querySelector("meta[name=viewport]").setAttribute('content', 'width=320.1');    
   myFun("divId");
   mTr();
}

function myFun(id)
{
   var obj = document.getElementById(id);
   if (obj)
   {
       obj.setAttribute("style", "height:300px;");
   }
}

function mTr()
{
   alert("Hello World!");
}
您可以在这里进行测试:
有几个原因使得
myFun()
不会被调用:

  • window.screen.height
    不完全是568
  • document.querySelector(“meta[name=viewport]”。content=“width=320.1”生成导致脚本执行停止的脚本错误
  • 此代码之前存在导致执行停止的错误
  • 您的代码在加载DOM之前执行得太早,因此无法找到相关的DOM元素
  • 首先要做的是检查浏览器错误控制台,看看报告了哪些脚本错误(如果有)

    第二件事是在javascript调试器(大多数浏览器内置)中的
    if
    语句上设置一个断点,然后单步执行以下语句以查看发生了什么


    您还可以在代码中嵌入
    console.log(“xxx”)
    语句,在控制台中创建一条面包屑轨迹,告诉您发生了什么事情。

    那么,问题是什么?为什么函数myFun没有在JavaScript中调用,只要对象不为null,那么执行
    if(object)
    的计算结果就是true。