简体   繁体   English

试图使jQuery工作

[英]Trying to make jquery work

I am having a hard time getting any jquery code to work. 我很难让任何jquery代码正常工作。 I've tried some examples and none work for me. 我尝试了一些示例,但没有一个适合我。 Here is an example that sort of works in chrome from the C drive, but not from dropbox on the web, nor does it work at all in ie9. 这是一个示例,它可以在C驱动器的chrome中工作,但不能在Web的Dropbox中工作,也不能在ie9中工作。

When it is 'sort of' working on chrome from my C drive, it starts out displaying all the tabs instead of just Tab A as it is supposed to. 当从我的C驱动器上对chrome进行“某种程度”处理时,它开始显示所有选项卡,而不是原来的那样显示选项卡A。 Then after clicking one of the links it only shows the corresponding tab. 然后,在单击链接之一后,它仅显示相应的选项卡。

My end goal is to modify my website so that the whole page doesn't flash while reloading every time I click on a menu item. 我的最终目标是修改我的网站,以使每次单击菜单项时重新加载时整个页面都不会闪烁。

<!--  found at: http://jsfiddle.net/uFgtS/ -->

<script type='text/javascript' src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type='text/javascript'>

$(window).on('hashchange', function() {
    $('div.tab').hide();
    $(location.hash).show();
});

$('a.hash').on('click', function(e){
    e.preventDefault();
    location.hash = $(this).data('hash');
});
</script>

<a href="#A" data-hash="A" class="hash">A Link</a>
<a href="#B" data-hash="B" class="hash">B Link</a>
<a href="#C" data-hash="C" class="hash">C Link</a>

<div id="A" class="tab">Tab A</div>
<div id="B" class="tab hidden">Tab B</div>
<div id="C" class="tab hidden">Tab C</div>

You forgot : 你忘了 :

$(document).ready(function() {

    // your code goes here

});

so when attaching the click handler to a.hash , there's no such element in the DOM. 因此,将点击处理程序附加到a.hash ,DOM中没有此类元素。

If you place your jQuery before the elements that it affects you need to wrap it in 如果将jQuery放在影响它的元素之前,则需要将其包装

$(document).ready(function() {
    // Code to be run once the document is ready
}

Alternatively, place your scripts at the very end of the document body to make $(document).ready() redundant. 或者,将脚本放在文档正文的最后,以使$(document).ready()多余的。

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

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