简体   繁体   English

如何使用JS用包含文件覆盖td内容?

[英]How to overwrite td content with an include file with JS?

I have a menu with names of different htm files like this: 我有一个菜单,其中包含不同的htm文件的名称,如下所示:

在此处输入图片说明

I want when I click in one, the content of a td shows the document that I just clicked, I have a for that list this names, and set the calling to the JS function: 我想要当我单击一个时,TD的内容显示了我刚刚单击的文档,我有一个用于该列表的名称,然后将调用设置为JS函数:

<% for (int i=0; i < fileNames.length; i++) { %>
    <tr> 
        <td onClick="refreshContent(this)"> <%= fileNames[i] %> </td>
    </tr>
<% } %>

I'm using a Jsp that displays the content of the first document by default: 我正在使用一个Jsp,默认情况下显示第一个文档的内容:

<td id="documentContaner">
    <%@ include file ="/docs/document1.htm" %>
</td>

I'd like something like this in my JS Function: 我想在我的JS函数中这样的事情:

function refreshContent(element) {
    var name = element.textContent;
    var tdContaner = document.getElementById("documentContaner");
    tdContaner.innerHTML = '<%@ include file ="/docs/'+ name +'.htm" %>';
}

first question: Is it possible do it this way? 第一个问题:有可能这样做吗? or Do I need to refresh the page to show new content? 还是需要刷新页面以显示新内容? second question: If it's possible, How can I write an include file inside the TD? 第二个问题:如果可能,如何在TD内写入包含文件?

You cannot use JavaScript to insert server side code into the DOM. 您不能使用JavaScript将服务器端代码插入DOM。 Server side code needs to be executed by the appropriate runtime / interpreter on the server before the results are delivered to the browser. 在将结果传递到浏览器之前,服务器端代码需要由服务器上的相应运行时/解释器执行。

To achieve this type of effect, you would need to write an HTTP endpoint which returns just the content of that cell and then use Ajax (usually via the XMLHttpRequest object) to fetch it, and then use DOM methods to insert the result into the cell. 为了实现这一类型的效果,你需要写一个HTTP端点返回该小区的只是内容,然后(通常通过XMLHttpRequest对象)使用Ajax来获取它,然后使用DOM方法的结果插入到细胞。

You can send AJAX from page to load file 您可以从页面发送AJAX到加载文件

<script>
    function refreshContent(element) {
        var name = element.textContent;
        var xhr= new XMLHttpRequest();
        xhr.open('GET', '/docs/'+ name +'.htm', true);
        xhr.onreadystatechange= function() {
            if (this.readyState!==4) return;
            if (this.status!==200) return; // or whatever error handling you want
            element.innerHTML= this.responseText;
        };
        xhr.send();
    }
</script>
<table>
    <tr>
        <td onClick="refreshContent(this)">test</td>
    </tr>
</table>

if you have jquery on your page use it 如果页面上有jQuery,请使用它

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    $(function () {
        $('#table-id').on('click', 'td.clickable', function (event) {
            event.preventDefault();
            var $element = $(this);
            var name = $element.text();
            $.get('/docs/'+ name +'.htm', function (response) {
                $element.html(response);
            })
        });
    })
</script>
<table id="table-id">
    <tr>
        <td class="clickable">test</td>
    </tr>
</table>

UPD. UPD。 or preload file & name and show/hide them 或预加载文件和名称并显示/隐藏它们

<script>
    function refreshContent(element) {
        var name = element.children[0];
        var file = element.children[1];
        name.style.display = 'none';
        file.style.display = null;
    }
</script>
<table>
    <tr>
        <% for (int i=0; i < fileNames.length; i++) { %>
    <tr>
        <td onClick="refreshContent(this)">
            <div class="name"><%= fileNames[i] %></div>
            <div class="file" style="display: none;"><%@ include file ="/docs/document1.htm" %></div>
        </td>
    </tr>
    <% } %>
    </tr>
</table>

with jquery 用jQuery

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    $(function () {
        $('#table-id').on('click', 'td.clickable', function (event) {
            var $element = $(this);
            var $name = $element.find('.name');
            var $file = $element.find('.file');
            $name.hide();
            $file.show();
        });
    })
</script>
<table id="table-id">
    <tr>
        <% for (int i=0; i < fileNames.length; i++) { %>
    <tr>
        <td class="clickable">
            <div class="name"><%= fileNames[i] %></div>
            <div class="file" style="display: none;"><%@ include file ="/docs/document1.htm" %></div>
        </td>
    </tr>
    <% } %>
    </tr>
</table>

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

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