简体   繁体   English

将HTML / JavaScript代码分开

[英]Separate HTML/Javascript code into

I don't know what term for this is but it's like with PHP when you use 'includes' or when you store HTML in a variable and call it to print at a certain point in a HTML page. 我不知道这是什么术语,但是当您使用“ includes”或将HTML存储在变量中并调用它以在HTML页面的特定位置打印时,它就像PHP。

I want to better organize and tidy up my html page which to me is so cluttered I'm getting brain fog just navigating my way around my code. 我想更好地组织和整理我的html页面,这对我来说很混乱,以至于我在浏览代码的过程中出现了大脑雾。 I don't have a web server everything is running client side javascript. 我没有网络服务器,所有程序都在运行客户端javascript。 It's actually a HTA application. 它实际上是HTA应用程序。

I want for example to call a table that I've kept in a separate .html file into the Main html file. 例如,我想将保存在单独的.html文件中的表称为Main html文件。

so for example: 因此,例如:

//main html file
<table>
<tr>
   <td>"call external html file here"</td>
</tr>
</table>

or if there is a way with jquery or javascript to store chunks of html code into a variable then call it to print in main html, although I don't know if this will take up too much resources. 或者是否可以用jquery或javascript将html代码块存储到变量中,然后调用它以在主html中打印,尽管我不知道这是否会占用过多资源。 What are my options? 我有什么选择?

Thanks! 谢谢!

To load external HTML, use JQuery .load() : http://api.jquery.com/load/ 要加载外部HTML,请使用JQuery .load()http : //api.jquery.com/load/

Example: 例:

$('#result').load('ajax/test.html');

You can also separate your javascript into another file: 您还可以将javascript分成另一个文件:

<script src="yourjavascript_file.js" type="text/javascript"></script>

You can use the jQuery load function: 您可以使用jQuery load功能:

HTML: HTML:

<table>
    <tr>
        <td id="load"></td>
    </tr>
</table>

jQuery: jQuery的:

$("#load").load("http://link.to/file");

对于初学者来说,另一个选项可能是模板化: http : //api.jquery.com/category/plugins/templates/

In the main html file, you can use code such as 在主html文件中,您可以使用以下代码

next.body()

Then, in the file you want to call, you can use code such as 然后,在您要调用的文件中,可以使用如下代码

inherit file="base.html"

to call the main file. 调用主文件。

But it all depends which framework you are using 但这全取决于您使用的框架

除了上面的答案外, load函数还为您提供了极大的灵活性,不仅可以插入HTML块,还可以通过在链接到HTML文件后添加以空格分隔的选择器表达式来仅插入该块中的选择部分。 :

$("#tobefilled").load("foo.html #onlythiselement",function() {});

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

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