【问题标题】:HasChanged Not Working in JqueryHasChanged 在 Jquery 中不起作用
【发布时间】:2016-04-01 07:22:41
【问题描述】:

我是新手,所以我会尽量清楚, 我正在尝试使用 Jquery 和 AJAX 创建一个单页应用程序。但是,什么都没有发生,没有错误,什么都没有。

转到 main、second 和third 是指向:#/main、#/second、#/third

另外,first.html、second.html、third.html 是应该从中检索数据的页面。

请检查我的代码,如果有问题请告诉我:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
	<script>
			window.addEventListener('hashchange', function (){
			
				if(window.location.haschange === '#/main'){
				$.get( "first.html", function( data ) {
			  $( "#examples" ).html( data );
				})
			}
				
				if(window.location.haschange === '#/second'){
				$.get( "second.html", function( data ) {
			  $( "#examples" ).html( data );
				})
			}
				
				if(window.location.haschange === '#/third'){
				$.get( "third.html", function( data ) {
			  $( "#examples" ).html( data );
				})
			}
				
		});
	</script>
 <## Heading ##html>
<head>
<title>text</title>
</head>
<body>
	<ul>
		<li><button><a href="#/main">Go to Main</button></li>
		<li><button><a href="#/second">Go to Second</button></li>
		<li><button><a href="#/third">Go to Third</button></li>
	</ul>
	
	<div id="examples">
		
	</div>
	
</body>
</html>

<!-- begin snippet: js hide: false -->

【问题讨论】:

  • window.addEventListener('hashchange', 。我认为您在这里有一个很大的旧错字。
  • 我不太确定hasChanged 是否存在。

标签: javascript jquery html json ajax


【解决方案1】:

使用window.onhashchange = function(req){ /*your code*/ }

【讨论】:

    【解决方案2】:

    这是您想要的另一种方法。

    var currentPage = "#main" ;
    
    $('.page').on('click', function(event)
    {
        page =  $(event.target).children().first().attr('href') ;
        if ( page != currentPage ) 
        {
            currentPage = page ;
            alert(page) ;
            // your if conditions here 
        }
    });
    			
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <## Heading ##html>
    <head>
    <title>text</title>
    </head>
    <body>
    	<ul>
    		<li><button class="page"><a href="#main">Go to Main</a></button></li>
    		<li><button class="page"><a href="#second">Go to Second</a></button></li>
    		<li><button class="page"><a href="#third">Go to Third</a></button></li>
    	</ul>
    	
    	<div id="examples">
    		
    	</div>
    	
    </body>
    </html>
    
    <!-- begin snippet: js hide: false -->

    【讨论】:

      【解决方案3】:

      我发现代码没有问题的答案只是它只支持http服务器请求,或者另一种选择是使用angularJS

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-31
        • 2013-07-21
        • 2012-04-04
        • 2015-03-16
        • 2016-05-18
        • 2015-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多