javascript - ajax refresh problem

WBOY
Release: 2016-10-10 11:56:09
Original
1060 people have browsed it

I just want an effect similar to segmentfaul. Click on which partition to display the corresponding content. But the problem now is that I don’t know how to write the href link. I want the content to be displayed on the current page. Herf cannot be written #. If I write href="1. html" will display the data first and then jump. In this case, the page will be empty and the data cannot be displayed. What should we do? Please look carefully. I don't want to prevent the jump. I want to know how the effect of segmentfault is achieved. If you use ajax, there is no need to jump to refresh the page. But segmentfault has a jump refresh. How is this achieved?

<code>代码只是大概描述一下
1.html
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
       document.getElementById('java').onclick=function(){
          div.innerHTML=str;
       }
    </script>
</head>
<body>
<a href="#" id="java">java</a>
<a href="#" id="php">php</a>
<div></div>
</body>
</html></code>
Copy after login
Copy after login

Reply content:

I just want an effect similar to segmentfaul. Click on which partition to display the corresponding content. But the problem now is that I don’t know how to write the href link. I want the content to be displayed on the current page. Herf cannot be written #. If I write href="1. html" will display the data first and then jump. In this case, the page will be empty and the data cannot be displayed. What should we do? Please look carefully. I don't want to prevent the jump. I want to know how the effect of segmentfault is achieved. If you use ajax, there is no need to jump to refresh the page. But segmentfault has a jump refresh. How is this achieved?

<code>代码只是大概描述一下
1.html
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
       document.getElementById('java').onclick=function(){
          div.innerHTML=str;
       }
    </script>
</head>
<body>
<a href="#" id="java">java</a>
<a href="#" id="php">php</a>
<div></div>
</body>
</html></code>
Copy after login
Copy after login

Just write a separate url, it is either ajax refresh or get. There are many small sections that are actually not a big problem, and the backend is also very easy. Because the data formats are basically the same, just write a regular expression to match the requested URL when matching routes, then process the data in a controller, and return different data according to different URLs. Moreover, the front-end template does not need to change greatly and is highly reusable.

In the event handler function, you can prevent the browser's default behavior (the default behavior here is to jump) by calling preventDefault or return false. In addition, it is server rendering, not ajax.

Finally, it is recommended to buy this book and study systematically. These questions are really too basic. Even if you have not studied systematically, searching or checking the API is much more efficient than asking questions

No matter what the href of <a> is, you can prevent the default behavior in the event

The following is the jQuery code, the native code is almost the same, call preventDefault() of event, or return false.

<code class="javascript">$("#java").on("click", e => {
    e.preventDefault();
});</code>
Copy after login

But you can also directly set href so that it does nothing

<code class="html"><a href="javascript: void(0)" ...>...</a></code>
Copy after login

When you click on a page, the js code corresponding to this page sends an HTTP GET request to the background, and then the background returns the corresponding (such as java/php, etc.) data.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!