简体   繁体   中英

How to load a txt file into div tab using Jquery

I want to load a txt file into a div tab-content . In the below code I used a paragraph inside the tab-content . Instead of that paragraph, I want to display information of a text file. How can I do that using jquery?

<div class="create">
    <ul class="nav nav-tabs" id="myTab" style="width: 22%">
        <li class="active"><a href="#cpp">C++</a></li>
        <li><a href="#java">Java</a></li>
        <li><a href="#python">Python</a></li>
    </ul>
    <div class="tab-content">
        <div id="cpp" class="tab-pane fade in active">
            <h3>#Creating an array in C++</h3>
        </div>
        <div id="java" class="tab-pane fade">
            <h3>#Creating an array in Java</h3>
        </div>
        <div id="python" class="tab-pane fade">
            <h3>#Creating an array in Python</h3>
        </div>
    </div>
</div>
<!--#End of create div-->
<div class="insert">
    <ul class="nav nav-tabs" id="myTab2" style="width: 22%">
        <li class="active"><a href="#myTab2-cpp">C++</a></li>
        <li><a href="#myTab2-java">Java</a></li>
        <li><a href="#myTab2-python">Python</a></li>
    </ul>
    <div class="tab-content">
        <div id="myTab2-cpp" class="tab-pane fade in active">
            <h3>#Inserting an array in C++</h3>
        </div>
        <div id="myTab2-java" class="tab-pane fade">
            <h3>#Inserting an array in Java</h3>
        </div>
        <div id="myTab2-python" class="tab-pane fade">
            <h3>#Inserting an array in Python</h3>
        </div>
    </div>
</div>

Pretty Easy:

$.get( "your.txt", function( data ) {
  $( ".yourDestinationDivClass" ).html( data );
});

Use Jquery ajax like:

$.ajax({
    url : "helloworld.txt",
    dataType: "text",
    success : function (data) {
        $("#destination").html(data);
    }
});

First you need to add jQuery ajax and then You need to add a dataType option in that Ajax request. Please follow below code::

$.ajax({
    url : "helloworld.txt", // your txt file path
    dataType: "text",
    success : function (data) {
        $(".tab-content").html(data); // replace .tab-content if you want to put .txt file's data in other place.
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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