Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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
Jquery 如何正确检查点击事件?_Jquery_Html - Fatal编程技术网

Jquery 如何正确检查点击事件?

Jquery 如何正确检查点击事件?,jquery,html,Jquery,Html,我已经编写了两个函数来控制点击事件 jQuery <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>Class

我已经编写了两个函数来控制点击事件

jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>Classic Online Store</title>
<link rel="stylesheet" type="text/css" href="../css/login-form.css"  />
<link rel="stylesheet" type="text/css" href="../css/style.css" />
<link rel="stylesheet" type="text/css" href="../css/nav-menus.css"/>
<!--tabs in the accordion -->
<link rel="stylesheet" type="text/css" href="../css/acard.css"/>

<!--  the template script-->
 <script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<!-- side category accordion -->
 <script type="text/javascript" src="../js/custom.js"></script>
 <script type="text/javascript" src="../js/customlog.js"></script>
 <script type="text/javascript" src="sideacardeion/script.js"></script>

<script language="javascript" type="text/javascript">

$('#btn_Run').click(function(e) {
        alert("Hello! ShareChiWai.");

});
</script>

</head>

经典网店
$(“#btn_运行”)。单击(函数(e){
警惕(“你好!共享空间”);
});
HTML

我这里有菜单

然后是登录表单

<?php 

if ($_SESSION['Id'] > 0 ){

echo '<h1>Login Form</h1>

       <h2>You have loged in already<br/></h2>
       <br />
       <h3>Click logout to exit </h3>
       <br />
       <h3><a href="logout.php"> logout</a></h3>';


 } else {
 echo  '  <h1>Login Form</h1>

<div id="contact_form">  
 <form name="contact" action="">  
 <fieldset>        
    <label for="email" id="email_label"><font color="#FFFFFF">Email</font></label>  
    <input type="text" name="email" id="email" size="30" value="" class="text-input" />  
    <label class="error" for="email" id="email_error">This field is required.</label>  

    <label for="phone" id="phone_label"><font color="#FFFFFF">Password</font></label>  
    <input type="password" name="phone" id="phone" size="30" value="" class="text-input" />  
    <label class="error" for="phone" id="phone_error">This field is required.</label>  
   <input type="button" id="btn_Run" value="Run" />      

    <br />  
    </fieldset>  
    </form>  
 </div>  

';

if (isset($_SESSION['attmpt'])){
    $leftlog = $_SESSION['attmpt'] - 4;
    echo '<div class="atmpt" id="atmpt"><font color="#FF0000"> You have '.$leftlog.' more chanse(s)</font></div>';

    }
   }

  ?>

这一页的其余部分

这个什么都不做

我不知道我的点击事件功能有什么问题?你能告诉我如何正确地检查点击事件吗?我在谷歌上搜索了所有jQuery事件控制器,发现我的第一个函数应该可以正常工作,但事实并非如此

当我点击登录btn时,控制台中没有错误和警报


请帮助,提前谢谢。

请确保在运行代码时加载jQuery文件

您可以通过以下方式完成此操作:

if (window.jQuery) {  
    // jQuery library loaded  
} else {
    // jQuery library is not loaded
}
为了安全起见,如果未加载jQuery,您可以尝试重新加载:

if (!window.jQuery || typeof jQuery == 'undefined') {
  // create script Element and append in head of HTML
  var jqElement = document.createElement('script'); 
  jqElement.type = 'text/javascript';
  // Path to jquery.js file, eg. Google hosted version
  jqElement.src = '/path-to-your/jquery.min.js'; //or reference an alternate CDN link
  document.getElementsByTagName('head')[0].appendChild(jqElement);
}

是在页面加载后创建的按钮。无论如何,试试这个,让我知道会发生什么:

<script type="text/javascript">
$(document).ready(function () {
    $('body').on('click','#btn_Run',function(){
        alert("Hello! ShareChiWai.");
    });
});
</script>

$(文档).ready(函数(){
$('body')。在('click','btn_Run',函数()上{
警惕(“你好!共享空间”);
});
});

而不是
“body”
。相反,我们可以使用页面加载时出现的任何选择器。

我已通过将函数更改为

$(document).ready(function(e) {
$('#btn_Run').click(function(e) {
        alert("Hello! ShareChiWai.");

});
});

感谢您的帮助和尝试,非常感谢。:)

页面上是否同时有这两个按钮?是否在页面加载后将按钮
#btn_Run
添加到DOM中?在这种情况下,当应用单击处理程序时,按钮不存在,因此处理程序不会响应单击事件。@RGraham我打赌这是一个id问题。@RGraham我说的是在
document.ready
完成后按钮被动态添加到DOM中的情况。如果没有更多的源代码,或者您正在使用的示例演示(通过JSFIDLE),它只是猜测问题可能是什么。用更多的代码更新您的问题。在第二个代码中,您缺少:async,附加到脚本位置。(不是每个html都有头),你可以删除| |的第二部分。使用现代浏览器,不管你是否设置script.async=true,它都将异步执行。那么这应该是委托事件吗?正确的?没有。@skshoyeb以前版本的点击功能。是时候更新你的知识了。@BlackSheep:oops明白了。如果我没弄错的话,我想新的编辑会影响授权。如果我错了,你可以告诉我:)@skshoyeb我已经更改了它,但是还没有运气,这很奇怪,你有可能分享整个代码吗@用户3150692