JQuery和链接-奇怪的情况 一些文本链接1 <a href="http://anotherlink.com">link2</a>

JQuery和链接-奇怪的情况 一些文本链接1 <a href="http://anotherlink.com">link2</a>,jquery,hyperlink,click,handler,Jquery,Hyperlink,Click,Handler,但是当我点击链接1时,点击处理程序不会调用 在另一种情况下: $('a').click(function(){ console.log( 'achtung' ); }); 当我点击link2时,处理程序会调用,但link1仍然无法工作 你能解释一下:为什么 以下是更多代码: <div id="text-wrapper"> <div id="text"> <div id="content_close">close</div

但是当我点击链接1时,点击处理程序不会调用

在另一种情况下:

$('a').click(function(){
   console.log( 'achtung' );
});
当我点击link2时,处理程序会调用,但link1仍然无法工作

你能解释一下:为什么


以下是更多代码:

 <div id="text-wrapper">
    <div id="text">
       <div id="content_close">close</div>
       <div id="inner">
       <!--Here will be content--> <br />               
    </div>
    ...
 </div>
谢谢大家,问题解决了。

当我换衣服时

 console.log( 'achtung' );

这对我来说就像预期的那样

也许你的控制台运行不稳定,或者你用来查看它的任何东西都不能正常工作



试验
一些文本

/**/


jQuery代码是否包装在
$(document.ready(function(){…})

如果您没有等待DOM就绪,那么jQuery可能无法找到
#internal

$(document).ready(function(){
    $('#inner a').click(function(){
        console.log( 'achtung' );
    });
});

凭直觉,尝试将“return;”或“debugger;”作为函数的最后一行。我想知道点击链接是否会导致控制台被清除。

您的
内部ID在页面中是唯一的吗


您可以尝试使用
document.getElementById(“内部”)
在Firebug中查询它,并查看返回的DOM节点是否是您期望的(将结果悬停在Firebug中,il将点亮页面中的DOM节点)。

有两种方法可以实现此目的。 必须加载HTML或仅加载节点。要实现这一点,您必须检查页面是否已加载或在DOM完全更新后执行JavaScript

如果在
中指定javascript,如下所示:

<script type="text/javascript">
  $('#inner a').click(function(){
    console.log( 'achtung' );
  });
</script>
另一种方法是将内联JavaScript放在domnode之后

<div id="inner">
  <a href="/test.html">link1</a>
</div>
<a href="/test2.html">link2</a>`
<script type="text/javascript">
  /*<![CDATA[*/
  $('#inner a').click(function(){
    alert( 'achtung' );
  });
  /*]]>*/
</script>

`
/**/

这些代码没有任何错误(尽管我注意到您允许在处理程序运行后仍然遵循链接地址,这似乎是故意的)。最佳猜测:内部的标记有问题。页面是否验证?您确定这两段代码都实际运行了吗?本质上,这段代码看起来非常好。我建议您尝试单独运行此代码,并确保没有任何干扰。此代码包含在页面中的什么位置—页脚、页眉或外部文件?这可能是您的问题—将代码放在$(document).ready()块中。什么都不会发生。我还尝试在FireBug中设置断点。仍然不起作用。我添加了完整的测试代码。我还在Unbuntu上运行FF 3.5.3。您正在运行什么?您还可以使用其他浏览器尝试您的代码和/或关闭所有扩展。如果可能,最好将所有Javascript放在页面末尾。这是因为浏览器在遇到JS时会进入同步模式。通过将代码放在页面的末尾,可以更快地显示内容,给用户一种页面加载速度更快的印象。没有必要将代码包装到doc.ready函数中,除非假设用户一看到链接就会单击该链接。如果您的Javascript位于页面的末尾,那么等待DOM准备就绪只需保护自己免受奇怪/奇怪/怪异的浏览器错误的攻击。什么都不会发生。但当我点击内部的链接时,FireBug控制台在几秒钟内“跳转”起来,然后返回。听起来好像jquery点击事件没有绑定到链接。尝试向链接添加一个id,并使用它来选择锚元素,看看这是否有区别。我现在没有选择了!
<!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" xml:lang="en" lang="en">
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
</head>
<body>
<div id="inner">some text <a href="#contacts">link1</a></div>
<p><a href="http://anotherlink.com">link2</a> <script type="text/javascript">
/*<![CDATA[*/
$('#inner a').click(function(){
   alert( 'achtung' );
});
/*]]>*/
</script></p>
</body>
</html>
$(document).ready(function(){
    $('#inner a').click(function(){
        console.log( 'achtung' );
    });
});
<script type="text/javascript">
  $('#inner a').click(function(){
    console.log( 'achtung' );
  });
</script>
// simplest form, not the best (without jQuery)
window.onload = function () {
  // search for the node with jQuery
  $('#inner a').click(function(){
    alert( 'achtung' );
  });
}

// jQuery form, best
$(document).ready(function () {
  // search for the node with jQuery
  $('#inner a').click(function(){
    alert( 'achtung' );
  });
});

// smallest jQuery from, also best
$(function () {
  // search for the node with jQuery
  $('#inner a').click(function(){
    alert( 'achtung' );
  });
});
<div id="inner">
  <a href="/test.html">link1</a>
</div>
<a href="/test2.html">link2</a>`
<script type="text/javascript">
  /*<![CDATA[*/
  $('#inner a').click(function(){
    alert( 'achtung' );
  });
  /*]]>*/
</script>