简体   繁体   English

PHP页面内的Javascript方法调用

[英]Javascript method call inside PHP page

I have a PHP page in which am hiding a div using a javascript method. 我有一个PHP页面,其中正在使用javascript方法隐藏div。 This method is called on a hyperlink click. 在超链接单击上调用此方法。 The issue is its giving Uncaught Reference error. 问题在于其给出了“未引用参考”错误。 Not sure what is the cause. 不知道是什么原因。 I tried even to have a external JS file and define the method in that, but still not working. 我什至试图拥有一个外部JS文件并在其中定义方法,但仍然无法正常工作。

I'll give you a hint: 我会给你一个提示:

make sure you have defined your DIV tag before the action script 确保在动作脚本之前定义了DIV标签

like: 喜欢:

<html>
 <head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="hideDiv">Hide on click</div>
<input type="button" value="hide" onClick="javascript:HideIt()"/>
<script type="text/javascript" language="javascript">

function HideIt(){
 //what ever it is:Hide codes goes here
 $('#hideDiv').hide() // we used it after the defined it :)
 }
</script>
</body>
</html>

Uncaught Reference error in javascript usually means that the object or accessor isn't defined at the time of calling the method. javascript中未捕获的引用错误通常意味着在调用方法时未定义对象或访问器。

I've seen it when using jQuery when jQuery is included at the bottom of the html and the javascript is being triggered before it. 我已经在使用jQuery的时候看到它,而jQuery包含在html的底部,而javascript在它之前被触发。

So 所以

$('divid').text = "blah";

before the 之前

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Hence the $ method doesn't exist. 因此,$方法不存在。

The solution would be to put the method call on body load. 解决方法是将方法调用置于身体负荷上。

Here is the general code to hide any div onClick. 这是隐藏任何div onClick的通用代码。

<html>
    <head>
        //Here to add script tag, which i have writen below
    </head>
    <body>
        <div id="testDiv">This is to be hide on click</div>
        <br/>
        <br/>
        <a href="#" onClick="$('#testDiv').hide();">Click to hide!</a>
        <br/>
        <br/>
        <a href="#" onClick="$('#testDiv').show();">Click to show!</a>
    </body>
</html>

NOTE: You should write apostrophes carefully. 注意:您应该仔细写撇号。 Bcox ' and " have different impact Bcox和产生不同的影响

Here is the script tag which u have to place in head tag: 这是您必须放在head标签中的script标签:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM