简体   繁体   English

在Javascript的“字符串”中链接/导入HTML文档

[英]Link/Import HTML Document in Javascript's “string”

I have this JavaScript function: 我有这个JavaScript函数:

function getsomethingelse()
{
    //erzeugt den HTML-Code
    var string;
    string  = "<b>This is a site</b>"

    //beschafft eine referenz auf das <div> element auf der seite
    myDiv   = document.getElementById("container");
    //lädt Inhalt in das <div> element
    myDiv.innerHTML = string;
}

wich is linked by this link 通过该链接链接

<a href="#" onclick="getsomethingelse()" class="panel">Link</a>

I am searching for a way to - instead of writing the whole HTML document into string= - import an external (HTML) file into this; 我正在寻找一种方法-而不是将整个HTML文档写入string =-将外部(HTML)文件导入其中; because it is quite chaotic to write a whole HTML document in a single line. 因为在一行中编写整个HTML文档非常混乱。

Can someone help me with this? 有人可以帮我弄这个吗? Thanks a lot in advance, benny 谢谢你,本尼

1-You can bring the code by ajax 2-If you work with PHP, you can create a <script src="phpjs.php"> and read the contents of the file: 1,你可以通过把代码阿贾克斯2 -如果你使用PHP工作,你可以创建一个<script src="phpjs.php">和读取文件的内容:

var dataHTML  = <? php echo fileContainerHTML; ?> 

3-If you can work with you to make a jQuery jQuery ( "# cont"). Load (...) 3-如果可以与您一起制作jQuery jQuery ( "# cont"). Load (...) jQuery ( "# cont"). Load (...)

是的,使用ajax从服务器中获取其他内容,然后将该内容分配给innerHTML属性。

Yes what you are asking is possible, you can do it using ajax 是的,您要问的是可能的,您可以使用ajax来完成

First of all if you are doing any javascript I recommend using jQuery http://jquery.com/ 首先,如果您正在使用任何JavaScript,建议您使用jQuery http://jquery.com/

There is a tutorial for ajax without jquery here: http://www.w3schools.com/Ajax/Default.Asp 这里有一个没有jquery的ajax教程: http : //www.w3schools.com/Ajax/Default.Asp

Here are some jQuery Tutorials: http://docs.jquery.com/Tutorials 以下是一些jQuery教程: http : //docs.jquery.com/Tutorials

A simpler way to achieve the same thing may be to use an iframe which you simply point at whatever html page, it also means you don't have to strip out the html and body tag. 实现同一目标的一种更简单的方法可能是使用一个iframe,您只需将其指向任何html页面即可,这也意味着您不必去除html和body标签。

Also you could have multiple divs with the html you need and show/hide the one(s) required. 另外,您可以将多个divs与所需的html并显示/隐藏所需的html。

Good luck 祝好运

You can Use Ajax for this. 您可以为此使用Ajax。

$("#container").load("/neededUrl.html"); $(“#container”)。load(“ / neededUrl.html”);

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

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