从php调用js函数的问题

时间:2021-01-14 16:03:48

I am using my php to call a js function like this:

我正在使用我的php调用这样的js函数:

<?php 
$chk=1;
if($chk==1)
{
    echo '<script>testing();</script>';
}
?>

and my js looks like:

我的js看起来像:

function testing()
{
   document.getElementById("mainbody").innerHTML="This is my first JavaScript!";
}

The js is an external js file.

js是一个外部js文件。

My html looks like:

我的HTML看起来像:

<html>
<head>
<script src="qotw.js"></script>
</head> 
    <body>
    <div id="mainbody"></div>     
    </body>
</html>

but this is not working. What am i doing wrong over here? Please tell me if you know.

但这不起作用。我在这做错了什么?如果你知道,请告诉我。

Best Zeeshan

3 个解决方案

#1


Try testing the following in order:
first: Your script is being called

尝试按顺序测试以下内容:首先:调用您的脚本

<script type="text/javascript">alert("hello");testing();</script>

second: The function is being called

第二:正在调用该函数

function testing() {
    alert('inside testing');
}

third: As suggested above,

第三:如上所述,

if (document.getElementById("mainbody") == null) alert('yep, its null');

then let us know the results

然后告诉我们结果

#2


You run the script before the div you are targeting exists, so the document.getElementById call returns a false value instead of an HTMLElementNode.

您在目标div存在之前运行脚本,因此document.getElementById调用返回false值而不是HTMLElementNode。

You either need to move the script element so it is after the div, or assign the function to an event handler (onload for instance) instead of calling it directly.

您需要移动脚本元素,使其位于div之后,或者将函数分配给事件处理程序(例如onload),而不是直接调用它。

This has nothing to do with the use of PHP.

这与PHP的使用无关。

Incidentally, your HTML is invalid and triggers quirks mode.

顺便说一句,您的HTML无效并触发怪癖模式。

#3


Make sure that 'testing' isn't called above 'mainbody' in the code, because then the code would be run at a point during load, when the object doesn't exist.

确保在代码中没有在'mainbody'上面调用'testing',因为代码将在加载期间的某个点运行,此时对象不存在。

#1


Try testing the following in order:
first: Your script is being called

尝试按顺序测试以下内容:首先:调用您的脚本

<script type="text/javascript">alert("hello");testing();</script>

second: The function is being called

第二:正在调用该函数

function testing() {
    alert('inside testing');
}

third: As suggested above,

第三:如上所述,

if (document.getElementById("mainbody") == null) alert('yep, its null');

then let us know the results

然后告诉我们结果

#2


You run the script before the div you are targeting exists, so the document.getElementById call returns a false value instead of an HTMLElementNode.

您在目标div存在之前运行脚本,因此document.getElementById调用返回false值而不是HTMLElementNode。

You either need to move the script element so it is after the div, or assign the function to an event handler (onload for instance) instead of calling it directly.

您需要移动脚本元素,使其位于div之后,或者将函数分配给事件处理程序(例如onload),而不是直接调用它。

This has nothing to do with the use of PHP.

这与PHP的使用无关。

Incidentally, your HTML is invalid and triggers quirks mode.

顺便说一句,您的HTML无效并触发怪癖模式。

#3


Make sure that 'testing' isn't called above 'mainbody' in the code, because then the code would be run at a point during load, when the object doesn't exist.

确保在代码中没有在'mainbody'上面调用'testing',因为代码将在加载期间的某个点运行,此时对象不存在。