Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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.click函数不工作_Jquery - Fatal编程技术网

jQuery.click函数不工作

jQuery.click函数不工作,jquery,Jquery,我有一段HTML,当点击链接时,div内容就变成了链接的div内容。第一个函数运行良好。在第二次点击hidelink时,jQuery似乎没有响应。我错过了什么 <div id="right"> <div id='titletext'><p>||||||||||||||</p></div> <div id='presentation'></div> <div class='hidet

我有一段HTML,当点击链接时,div内容就变成了链接的div内容。第一个函数运行良好。在第二次点击hidelink时,jQuery似乎没有响应。我错过了什么

<div id="right">
    <div id='titletext'><p>||||||||||||||</p></div>
    <div id='presentation'></div>

    <div class='hidethisdiv'>
        <div id ="years">
            <h4><a id='resourceslink' href='#resources'>2010 Presentations</a></h4>
        </div>
        <div id='resources'>    
            <h4><a id='resourceslink' href='#resources'>2010 Presentations</a></h4>
            <p><a id='hidelink' href='#years'>&laquo;--back</a></p>
        </div>
    </div>
</div>

<script type="text/javascript">// <![CDATA[
  $('#mainmenu').fadeIn(2000, function() {
    // Animation complete.
  });

$('#presentation').html($('#years').html());

$( function() {
  $("#resourceslink").click(
    function () {
    $('#presentation').html($('#resources').html());
    }
   );

  $("#hidelink").click(
  function (){
    $('#presentation').html($('#years').html());
  }
  );
  //just add more divs like resources and a hidelink for new conferences
});
</script>

||||||||||||||

//
在显示的代码中,id和值之间有一个空格;)此外,id是唯一的,因此必须使用id=“resourceslink”来表示a是一种不好的做法:)

使用
$(“#resources”).html()
将元素转换为html。你以后会读到的。问题是在解析html之前,您将click事件绑定到了
#hidelink
。事件将链接到隐藏的元素

一种解决方案是在将内容添加到
#演示文稿
后链接单击事件。另一个问题是,将html添加到
#presentation
后,
#hidelink
不再是唯一的

更好的解决方案是不使用
.html()
。将所有元素添加到
#演示文稿中
,并使用
display:none
隐藏它们。然后绑定单击事件,并使用
.show()
.hide()
显示
#年份
#资源

例如:

<div id="right">
    <div id='presentation'>
        <div id ="years">
            <h4><a id='resourceslink' href='#resources'>2010 Presentations</a></h4>
        </div>
        <div id='resources' style="dislay: none">    
            <h4><a id='resourceslink' href='#resources'>2010 Presentations</a></h4>
            <p><a id='hidelink' href='#years'>&laquo;--back</a></p>
        </div>
    </div>
</div>


$("#resourceslink").click(function () {
    $('#resources').show();
    $('#years').hide();
});

$("#hidelink").click(function () {
    $('#resources').hide();
    $('#years').show();
});

$(“#资源链接”)。单击(函数(){ $(“#资源”).show(); $(“#年”).hide(); }); $(“#hidelink”)。单击(函数(){ $(“#资源”).hide(); $(“#年”).show(); });
您也可以使用jquery隐藏和显示,如下所示

$("#hidelink").live('click',function () {

$('#resources').hide();

$('#years').show();

});
如果你使用firefox作为浏览器,你可以使用firebug,它是一个附加组件。通过在使用firebug的脚本上添加断点,你可以了解出了什么问题


希望这对您有所帮助。

首先,ID是唯一的。它必须只存在一次!在您的示例中,有2个resourceslink

如果要对多个元素进行分组,请使用类。提醒:每个元素可以有多个类!比如说

<a href="#hey" class="testing hello">heyhey</a>
并像这样附加事件侦听器

$(".testing").on("click", function() { 
    doThis();
})

似乎您的最后一个
$(function(){…
块尚未启动。请尝试删除
$(function()
部分,然后运行那些
$(“资源链接”)。单击(…)
。如果需要包装,请尝试
($(function(){…})()
希望这能有所帮助。另外,
id
不能多次分配,请改用
class
。为什么会发生这种情况,我该如何解决?不要使用.live(),它已被弃用:而且根本没有性能。
$(".testing").on("click", function() { 
    doThis();
})