Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 - Fatal编程技术网

JQuery-无法在锚上生成单击事件

JQuery-无法在锚上生成单击事件,jquery,Jquery,这是一个有点奇怪的问题 我的头HTML看起来像 <div class="nav-collapse collapse "> <!-- .nav, .navbar-search, .navbar-form, etc --> <ul class="nav pull-right"> <li style="floa

这是一个有点奇怪的问题

我的头HTML看起来像

<div class="nav-collapse collapse ">
                        <!-- .nav, .navbar-search, .navbar-form, etc -->
                        <ul class="nav pull-right">
                            <li style="float: left;"><a href="home.html">Home</a></li>
                            <li style="float: left;padding-left: 30pt;"><a id="selDb" href="selectDatabase.html">Do Something</a>
                            </li>
                            <li style="float: left;padding-left: 30pt;"><a href="coming-soon.html">Refresh</a></li>
                            <li style="float: left;padding-left: 30pt;"><a href="coming-soon.html">Start XYZ</a></li>
                            <li style="float: left;padding-left: 30pt;"><a id="signOut" href="#">Sign Out</a></li>
                        </ul>
                    </div>
当我取消注释警报($('A#signOut'); 我可以得到警报,但当我删除它时,我没有得到警报


有什么线索吗?

这可能是负载的异步性质的问题。在加载的完整回调中执行ajax调用怎么样?这样就可以保证锚点a#注销存在,因为您在加载完成后正在执行ajax

$(document).ready(function() {
$('#head').load('header.html', function(){

   $.ajax({
    type: 'get',
    url: 'someUrl/signout',
    cache: 'false',
    success: function(data) {
        $('A#signOut').attr('HREF', data);
    },
    error: function(error) {
        alert("Internal server error.");
    }
});

});

});
作品:


$(文档).ready(函数(){ $('#head').load('header.html'); $.ajax({ 键入:“get”, url:'someUrl/注销', 缓存:“false”, 成功:功能(数据){ $('a#signOut').attr('href',data); }, 错误:函数(错误){ 警报(“内部服务器错误”); } }); }); $('a#signOut')。单击(函数(){ 警报(“已单击注销”); });

应该在
.ready()
回调中

我想在单击锚标记时进行ajax调用。在url上的第一个Ajax调用:“someUrl/signout”正在设置链接上的url,而我想在单击链接时再调用一次。谢谢。这对我很有用。$('head').load('header.html',function(){$('A#signOut')。单击(function(){alert('signOut Clicked');});没问题。还可以看看这个->a#signOut.attr('href',data);
$(document).ready(function() {
$('#head').load('header.html', function(){

   $.ajax({
    type: 'get',
    url: 'someUrl/signout',
    cache: 'false',
    success: function(data) {
        $('A#signOut').attr('HREF', data);
    },
    error: function(error) {
        alert("Internal server error.");
    }
});

});

});
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

</head>
<body>

<div class="nav-collapse collapse ">
<!-- .nav, .navbar-search, .navbar-form, etc -->
<ul class="nav pull-right">
<li style="float: left;"><a href="home.html">Home</a></li>
<li style="float: left;padding-left: 30pt;"><a id="selDb" href="selectDatabase.html">Do Something</a></li>
<li style="float: left;padding-left: 30pt;"><a href="coming-soon.html">Refresh</a></li>
<li style="float: left;padding-left: 30pt;"><a href="coming-soon.html">Start XYZ</a></li>
<li style="float: left;padding-left: 30pt;"><a id="signOut" href="#">Sign Out</a></li>
</ul>
</div>

<script type="text/javascript">

$(document).ready(function() {
$('#head').load('header.html');
$.ajax({
    type: 'get',
    url: 'someUrl/signout',
    cache: 'false',
    success: function(data) {
        $('a#signOut').attr('href', data);
    },
    error: function(error) {
        alert("Internal server error.");
    }
});

});

$('a#signOut').click(function() {
alert('Signout Clicked');
});

</script>

</body>
</html>
$('a#signOut').click(function() { alert('Signout Clicked'); });